function class or module.__name__
suggest changeThe special attribute __name__ of a function, class or module is a string containing its name.
import os
class C:
pass
def f(x):
x += 2
return x
print(f)
# <function f at 0x...>
print(f.__name__)
# f
print(C)
# <class '__main__.C'>
print(C.__name__)
# C
print(os)
# <module 'os' (frozen)>
print(os.__name__)
# os
A module loaded from a file includes that path instead of (frozen). The address in the function repr changes between runs.
The __name__ attribute is not, however, the name of the variable which references the class, method or function, rather it is the name given to it when defined.
def f():
pass
print(f.__name__)
# f - as expected
g = f
print(g.__name__)
# f - even though the variable is named g, the function is still named f
This can be used for debugging.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents