python how to get function name 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: how to get calling function in python
def Square(X):
return (X * X)
def SumofSquares(Array, n):
Sum = 0
for i in range(n):
SquaredValue = Square(Array[i])
Sum += SquaredValue
return Sum
Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = len(Array)
Total = SumofSquares(Array, n)
print("Sum of the Square of List of Numbers:", Total)
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()