• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Get Name of Function in Python

Get Name of Function in Python

July 14, 2022 Leave a Comment

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.

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.

Other Articles You'll Also Like:

  • 1.  Convert String to List Using Python
  • 2.  Using Python to Replace Multiple Spaces with One Space in String
  • 3.  Check if Number is Between Two Numbers Using Python
  • 4.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 5.  Python Decrement Counter with -= Decrement Operator
  • 6.  pandas Correlation – Find Correlation of Series or DataFrame Columns
  • 7.  pandas groupby size – Get Number of Elements after Grouping DataFrame
  • 8.  Get Substring Between Two Characters with Python
  • 9.  pandas product – Get Product of Series or DataFrame Columns
  • 10.  Calculating Sum of Squares 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.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy