Determining the name of the current function in Python
A function can be written as a block of code, as a statement, or in a combination of both. Python lets us do any of these with ease. When we call a function, it is always the current function that we want to determine. We can use the function’s name as a way of doing so.
In this tutorial, I will show you how to get the name of the current function in Python.
Function Statements
Functions are blocks of code that can be reused. They are written like this:
def function_name(argument_1, argument_2,...):
statement_list
return_value
This function statement creates a new function with the given function name. The function then executes the statements within the function body. After the function is executed, its return value will be returned to whatever called the function.
As you can see, functions can contain multiple statements. Each statement runs when the function is called. This makes it useful to track the name of the current function, since we want to be able to identify which function is being called at a particular moment.
Python functions are named using the same syntax as variable names. For example:
my_function = function_name()
When a function is defined in a file, the first line of the file will automatically become the name of the function.
The name of the current function is accessed through the special name __name__. The name of the function is retrieved as follows:
def my_function():
print(__name__)
The function __name__ is special and only exists within functions. As the name implies, this attribute provides information about the function that we are currently in. There are several attributes that provide information about the function that we are currently in. The attributes we are most likely to use include __doc__, __module__, and __qualname__.
The docstring is just what it sounds like. The docstring contains information about the function. You can see this information in the interpreter by entering help(function_name).
__module__ tells us which module this function is part of.
__qualname__ tells us the name of the current function. The name of the current function is the same as the name of the function.
Function Blocks
Functions are blocks of code. If we write a function like this,
def my_function():
pass
The entire block of code is considered the function. We could use this function as follows:
def my_function():
pass
def function_name(argument_1, argument_2,...):
statement_list
return_value
print(__name__)
def another_function():
my_function()
print("I am in another function.")
another_function()
The first function is called in the first line of the file. The second function is called inside the first function. The first function returns an empty string, which is printed when the function is called. The second function prints a message.
A function can be defined by using the def keyword followed by the function’s name and then a colon and the parameters. The parameters are comma-separated lists of the parameters that we want to pass to the function. The parameters are passed to the function by the function’s call.
The following shows how the name of the function, the arguments, and the return value are passed to the function.
def my_function(a, b, c, d, e, f, g):
pass
This is how we would call the my_function function with the arguments a, b, c, d, e, f, and g.
The arguments are always passed by position. That is, we pass the first argument to the function first, the second argument second, and so on. This allows the function to access the arguments in order.
The return value of the function is returned when the function is called. We can see this in the following example.
def my_function():
pass
def my_function(a, b, c, d, e, f, g):
return a * 2 + b * 3 + c * 4 + d * 5 + e * 6 + f * 7 + g * 8
This is how we would call the my_function function and pass the arguments a, b, c, d, e, f, and g. The return value of the function is multiplied by 2, then added to the sum of the arguments.
We can also define a function that returns multiple values by using a tuple. We do this by defining a list and then un-indenting a new line. For example:
def my_function(a, b, c, d, e, f, g):
pass
def my_function(a, b, c, d, e, f, g):
return a * 2, b * 3, c * 4, d * 5, e * 6, f * 7, g * 8
This is how we would call the my_function function and pass the arguments a, b, c, d, e, f, and g.
Another example: Python – Determining the name of the current function in Python
import inspect
def first():
print(inspect.currentframe().f_code.co_name)
print(inspect.stack()[0][3])
second()
def second():
print(inspect.currentframe().f_code.co_name)
print(inspect.stack()[0][3])
def main():
first()
main()