python get the name of a function code example
Example 1: give a function a name python
def my_function():
pass
class MyClass(object):
def method(self):
pass
print(my_function.__name__)
print(MyClass.method.__name__)
print(my_function.__qualname__)
print(MyClass.method.__qualname__)
Example 2: get name of a file in python
>>> f = open('/tmp/generic.png','r')
>>> f.name
'/tmp/generic.png'
>>> import os
>>> os.path.basename(f.name)
'generic.png'
Example 3: give a function a name python
for i in a:
print i.__name__
Example 4: python get function from string name
module = __import__('foo')
func = getattr(module, 'bar')
func()