Python get def name

Python How to get function name?

In this tutorial, we are going to learn how to get the function name in Python. Getting the name any function is a straightforward thing. We have two different ways for Python2 and Python3. Let’s see both of them.

Python2

Every function in Python2 has a property called func_name that gives you the name of the current function. Let’s see an example. Make sure you are using Python2 while executing the following example.

Example

# defining a function def testing_function(): """ This is a simple function for testing """ return None print("Function name is (Python2) '<>'".format(testing_function.func_name))

Output

If you execute the above program, then you will get the following result.

Function name is (Python2)'testing_function'

Python3¶

The function property func_name is deprecated in Python3. We will get the name of the using property __name__ of the function. Let’s see an example. Make sure you are using Python3 while running the following code.

Example

# defining a function def testing_function(): """ This is a simple function for testing """ return None print(f"Function name is (Python3) ''")

Output

If you execute the above program, then you will get the following result.

Function name is (Python3) 'testing_function'

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Источник

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. 

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.

Читайте также:  Php clone object to this

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

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() 

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.

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__) 

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__) 

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() 

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() 

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.

Читайте также:  Add key and value to javascript object

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.

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

Источник

How to Get a Function Name as a String in Python?

In this python tutorial, we will learn how to get a function name as a string.

Table Of Contents

Get a function name as a string using __name__

Python3 version supports this method which is used to get the function name in string format. It also returns the module name.

Here, the function is the function name. If you want to check the type of the returned function, then you can use the type() function. It returns the string class type.

Frequently Asked:

In this example, we will create two functions and get the function names and their types using __name__ .

# Create a function named my_first_function. def my_first_function(): print ("This is my first function") # Call the function my_first_function() # Get the function name as string print ("Function name: ", my_first_function.__name__) # Get the type print ("Function type: ", type(my_first_function.__name__)) # Create a function named my_second_function. def my_second_function(): print ("This is my second function") # Call the function my_second_function() # Get the function name as a string print ("Function name: ", my_second_function.__name__) # Get the type print ("Function type: ", type(my_second_function.__name__))

This is my first function Function name: my_first_function Function type: This is my second function Function name: my_second_function Function type:

You can see that the function name is returned and the type is str, which represents the string.

In this example, we imported two modules and get the module name and its type using name .

import math import random # Get the math module name as string print ("Function name: ", math.__name__) # Get the type of math print ("Function type: ", type(math.__name__)) # Get the random module name as string print ("Function name: ", random.__name__) # Get the type of random print ("Function type: ", type(random.__name__))

Function name: math Function type: Function name: random Function type:

You can see that the module name is returned and the type is str, which represents the string.

Get a function name as a string using func_name

Python2 version supports this method which is used to get the function name in string format. It is deprecated in the python3 version.

Here, the function is the function name. If you want to check the type of the returned function, then you can use the type() function. It returns the class type.

In this example, we will create two functions and get the function names and their types using func_name.

# Create a function named my_first_function. def my_first_function(): print ("This is my first function") # Call the function my_first_function() # Get the function name as string print ("Function name: ", my_first_function.func_name) # Get the type print ("Function type: ", type(my_first_function.func_name)) # Create a function named my_second_function. def my_second_function(): print ("This is my second function") # Call the function my_second_function() # Get the function name as a string print ("Function name: ", my_second_function.func_name) # Get the type print ("Function type: ", type(my_second_function.func_name))
This is my first function ('Function name: ', 'my_first_function') ('Function type: ', ) This is my second function ('Function name: ', 'my_second_function') ('Function type: ', )

You can see that the function name is returned and the type is str, which represents the string. This code will not work with python3, it will work with previous versions of Python.

Читайте также:  Open source css codes

Get a function name as a string using qualname

Python3 supports this method which is used to get the function name in string format. It also returns the names of the classes and methods.

Here, the function is the function name. If you want to check the type of the returned function, then you can use the type() function. It returns the class type.

In this example, we will create two functions and get the function names and their types using qualname .

# Create a function named my_first_function. def my_first_function(): print ("This is my first function") # Call the function my_first_function() # Get the function name as string print ("Function name: ", my_first_function.__qualname__ ) # Get the type print ("Function type: ", type(my_first_function.__qualname__ )) # Create a function named my_second_function. def my_second_function(): print ("This is my second function") # Call the function my_second_function() # Get the function name as a string print ("Function name: ", my_second_function.__qualname__ ) # Get the type print ("Function type: ", type(my_second_function.__qualname__ ))

This is my first function Function name: my_first_function Function type: This is my second function Function name: my_second_function Function type:

You can see that the function name is returned and the type is str, which represents the string.

Example 2:
In this example, we will create a class and get the module name and its type using name .

# Define a class with a method - hello class My_class(): def hello(self): pass # Get the class name as a string print ("Class name: ", My_class.__qualname__ ) # Get the class type print ("Class type: ", type(My_class.__qualname__ )) # Get the method name as a string print ("Method name: ", My_class.hello.__qualname__ ) # Get the method type print ("Method type: ", type(My_class.hello.__qualname__ ))

Class name: My_class Class type: Method name: My_class.hello Method type:

In the above code, we created a class named My_class, and a method-hello is created inside it. using qualname, we returned the function name as a string.

Summary

In this tutorial, we discussed three ways to return a function name as a string in python. The func_name does not work in the python 3 versions. If you want to get the class name and methods names as strings, you can use qualname .

Источник

Оцените статью