Python Print Function Name

In this article, we will learn how to get the function name and its importance in Python programming.

__name__ variable

Function names are an essential part of Python programming. They help us to keep our code organized and easy to read. When writing a function, we give it a name to refer to later. The function name is like a label for the function. It helps us to remember what the function does and makes our code easier to read.

Calling a function in python

When we call a function, we use the function name followed by parentheses.
For example, if we have a function named my_func(), we would call it my_func(). The parentheses are necessary because they tell Python we want to execute the code inside the function.

def func:
	print("Welcome to geekbits!!") 

func() # Calling the function.

Returns Welcome to geekbits!!

Python will not execute the code inside the function if we omit the parentheses when we call the function.
It can be confusing for beginners, so always include the parentheses when calling a function.

Naming Convention (Python function names)

Function names can be any length and contain letters, numbers, and underscores. They cannot start with a number, though. It is considered good practice to use descriptive names for our functions so that others can easily understand what our code does.

We've already seen how to use the print() function to print messages to the screen. Refer to this article.

https://www.geekbits.io/python-print-function/

Let's see how we can get the name of the function. We can do this by using the built-in __name__ variable.

Using the __name__ Variable in python

The __name__ variable contains the current module's name. When we run a Python program, the __name__ variable is set to __main__. If we import a module, the __name__ variable is set to that module's name.

Let's see how this works with the print() function. We can start by creating two python files. file1 and file2.

In file1, we will define the print() function.

print("I am %s" %__name__)

def func():
    print ("This is file 1")

if __name__ == "__main__":
    func()

Returns

I am _main_

This is file 1

After running this program, the __name__ variable is set to __main__. It's why the if statement executes and "This is file 1" is printed in the console.


In file2, we will import the module from file1 and run the program.

import file1

Returns

I am file1

Importing modules runs the program from the imported module.

As you can see, the __name__ variable is set to the name of the module that we imported. In this case, it is file1. However, the if statement in file1 does execute because the __name__ variable is not set to __main__. It is set to file1

Let's make some modifications to file1 and run the program.

import file2
print("I am %s" %__name__)

Returns

I am file1

I am _main_

As you can see, Running this program executes the imported module and then the print function. The __name__ variable is set to __main__, which is the current module's name. The if statement in file1 does not also execute because file1 does not run directly. This means that the __name__ variable is not set to __main__.

Now that we know how the __name__ variable works, let's see how we can use it to know the name of a function.

Get function name in python | __name__ variable

We can use the __name__ variable to get the name of any function, not just imported modules. For example, we can define our function and then print its name:

def func():
	print("Hello from Geekbits")

print(func.__name__)

Returns func

After running this program, we see that the name variable is set to 'func':

The __name__ variable can be very useful when we want to know the name of the currently executing function. For example, let's say we have a program that calls two functions, func1() and func2():

def func1():
	print("Hello from func1()")


def func2():
	print("Hello from func2()")

    
func1()

func2()

We can use the __name__ variable to print the name of the currently executing function:

def func1():
	print(func1.__name__)
	print("Hello from func1()")


def func2():
	print(func2.__name__)
	print("Hello from func2()")


func2()

func1()

Returns

func2

Hello from func2()

func1

Hello from func1()

After running this program, we see that the __name__ variable is set to 'func2' when func2() is executed and 'func1' when func1() is executed:


Here is another way to do the same thing.

import sys

def func1():
	print(sys._getframe().f_code.co_name)
	print("Hello from func1()")


def func2():
	print(sys._getframe().f_code.co_name)
	print("Hello from func2()")


func2()

func1()

Returns

func2

Hello from func2()

func1

Hello from func1()

As you can see, the __name__ variable can be very useful for debugging purposes. It can help us to know which function is currently being executed. This is helpful when we are working with large programs with many functions.

Conclusion

In this article, we've learned how to get the name of a function using the __name__ variable. We've also seen how this can be useful for debugging purposes. Questions?? Drop them in the comments.

Thanks for reading:)

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.