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

The Programming Expert

Solving All of Your Programming Headaches

  • 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.  Using Python to Sort Two Lists Together
  • 2.  Using Python to Generate Random String of Specific Length
  • 3.  Using Python to Find Second Largest Value in List
  • 4.  Perfect Numbers in Python
  • 5.  Union of Lists in Python
  • 6.  How to Read Pickle File from AWS S3 Bucket Using Python
  • 7.  Get First Key and Value in Dictionary with Python
  • 8.  pandas max – Find Maximum Value of Series or DataFrame
  • 9.  Python Round to Nearest 10 With round() Function
  • 10.  Using Python to Count Odd Numbers in List

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