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:
__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:
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:
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.
Comparison by language
| Language | Method | Example Output | Notes |
| Python | __name__ | my_function | Doesn't handle decorators transparently. |
| JavaScript | .name | myFunction | Returns an empty string for anonymous functions. |
| C# | Reflection/Expression Trees | MyMethod | Slightly 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.
Related reading
- How to get comparable and reproducible results from LogisticRegressionCV and GridSearchCV
- How to get dot product of two sparsevectors in Omn , where m and n are the number of elements in both vectors
- How to get dynamodb to only return certain columns
- How to get Memory cost of Distributed Map across node in hazelcast
- How to get the best model when using EarlyStopping callback in Keras?
- How to get the cpu usage per thread on windows win32
- How to have a processor intensive function update every tick of the metronome and draw to canvas using web workers?
- How to have different machines running different task?

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 courseData 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.