Programming
Function Names
Coding Tips
String Conversion
Code Optimization

How to get a function name as a string in Python, JavaScript and C#

Codemia Knowledge Hub ·

In Python, read function.__name__. In JavaScript, read the function's .name property. In C#, use reflection (MethodInfo.Name) or an expression tree. Getting the name as a string is handy for logging, debugging, and patterns such as command maps or factories.

Python

The main way to get a function's name in Python is the __name__ attribute.

Example:

python
1def my_function():
2    pass
3
4function_name = my_function.__name__
5print(function_name)  # Output: my_function

__name__ returns the name the function was defined with. Decorators can change it unless they are written carefully.

JavaScript

Function objects have a .name property that returns the function's name, or an empty string for anonymous functions.

Example:

javascript
1function myFunction() {}
2
3console.log(myFunction.name); // Output: myFunction
4
5const anonymousFunction = function() {};
6console.log(anonymousFunction.name); // Output: ""

Arrow functions and anonymous functions can give you an empty string, so name the function if you need to read its name later.

C#

C# takes more work, because method names are not normally handled as string literals. Reflection or expression trees give you the method name.

Using Reflection:

csharp
1using System;
2using System.Reflection;
3
4public class Example
5{
6    public void MyMethod() {}
7
8    public static void Main()
9    {
10        MethodInfo methodInfo = typeof(Example).GetMethod("MyMethod");
11        Console.WriteLine(methodInfo.Name); // Output: MyMethod
12    }
13}

Using Expression Trees:

Expression trees let you refer to a method without hard-coding its name as a string, which helps in cases such as INotifyPropertyChanged implementations.

csharp
1using System;
2using System.Linq.Expressions;
3using System.Reflection;
4
5static string GetMethodName<T>(Expression<Action<T>> expr)
6{
7    var method = ((MethodCallExpression)expr.Body).Method;
8    return method.Name;
9}
10
11// Usage
12string name = GetMethodName<Example>(x => x.MyMethod());
13Console.WriteLine(name); // Output: MyMethod

Comparison by language

LanguageMethodExample OutputNotes
Python__name__my_functionDoesn't handle decorators transparently.
JavaScript.namemyFunctionReturns an empty string for anonymous functions.
C#Reflection/Expression TreesMyMethodSlightly more complex, utilizes reflection or expression trees.

Where function names as strings are useful

  • Logging and debugging: knowing which function ran or threw an error speeds up debugging.
  • Dynamic invocation: when functions are called dynamically (for example in a command pattern), a map from names to implementations is useful.
  • APIs and libraries: documentation tools or custom API libraries can use function names to generate docs or routing rules.

The refactoring risk

Relying on names as strings weakens encapsulation, and renames are not always caught by tooling: renaming a function may not update string literals that refer to it. Use the technique where the application actually needs it.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms