Python function object name

Get Name of Function in Python

To get the name of a function when using Python, you can access the __name__ property.

def function_example(): pass print(function_example.__name__) #Output: function_example

If you are in a function and want to know the name of that function, then you can use the Python inspect module.

import inspect def function_example(): frame = inspect.currentframe() return inspect.getframeinfo(frame).function print(function_example()) #Output: function_example

When working in Python, the ability to get the names of different objects or functions can be useful.

One such example is if you want to get the name of a function.

To get the name of a function when using Python, you can simply access the __name__ property.

Below is a simple example showing you how to get the name of a function in Python.

def function_example(): pass print(function_example.__name__) #Output: function_example

Get Name of Function when Inside Function in Python

If you want to get the name of a function when you are already in a function, then you have to do a little more work.

To get the name of the function you are currently in, you can use the inspect module.

Читайте также:  Typescript массив удалить элемент

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects.

To get the name of the function you are currently in, you can use inspect to get the current frame with currentframe(), and then use the function property.

Below shows you how to get the name of the function you are currently in in Python.

import inspect def function_example(): frame = inspect.currentframe() return inspect.getframeinfo(frame).function print(function_example()) #Output: function_example

Hopefully this article has been useful for you to be able to learn how to get the name of a function using Python.

  • 1. Find Index of Maximum in List Using Python
  • 2. Convert False to 0 in Python
  • 3. Truncate Decimal from Number with Python math.trunc() Function
  • 4. Python Split List into N Sublists
  • 5. pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 6. Using Python to Add String to List
  • 7. pandas DataFrame size – Get Number of Elements in DataFrame or Series
  • 8. String Contains Case Insensitive in Python
  • 9. PROC FREQ Equivalent in Python
  • 10. Using Lambda Expression with max() in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Источник

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.

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 .

Источник

Get Function Name in Python

Get Function Name in Python

This tutorial will introduce how to get the function name in Python.

Use the __name__ Property to Get the Function Name in Python

In Python, every single function that is declared and imported in your project will have the __name__ property, which you can directly access from the function.

To access the __name__ property, just put in the function name without the parentheses and use the property accessor .__name__ . It will then return the function name as a string.

The below example declares two functions, calls them, and prints out their function names.

def functionA():  print ("First function called!")  def functionB():  print ("\nSecond function called!")  functionA() print ("First function name: ", functionA.__name__)  functionB() print ("Second function name: ", functionB.__name__) 
First function called! First function name: functionA  Second function called! Second function name: functionB 

Note that this solution also works with the imported and pre-defined functions. Let’s try it out with the print() function itself and a function from an imported Python module os .

import os  print("Function name: ", print.__name__) print("Imported function name: ", os.system.__name__) 
Function name: print Imported function name: system 

In summary, getting the function name in Python can easily be done by using the function property __name__ , a string property containing the function name without the parentheses.

Skilled in Python, Java, Spring Boot, AngularJS, and Agile Methodologies. Strong engineering professional with a passion for development and always seeking opportunities for personal and career growth. A Technical Writer writing about comprehensive how-to articles, environment set-ups, and technical walkthroughs. Specializes in writing Python, Java, Spring, and SQL articles.

Источник

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