Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3609950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:41:30+00:00 2026-05-18T21:41:30+00:00

How can I get a variable that contains the currently executing function in Python?

  • 0

How can I get a variable that contains the currently executing function in Python? I don’t want the function’s name. I know I can use inspect.stack to get the current function name. I want the actual callable object. Can this be done without using inspect.stack to retrieve the function’s name and then evaling the name to get the callable object?

Edit: I have a reason to do this, but it’s not even a remotely good one. I’m using plac to parse command-line arguments. You use it by doing plac.call(main), which generates an ArgumentParser object from the function signature of “main”. Inside “main”, if there is a problem with the arguments, I want to exit with an error message that includes the help text from the ArgumentParser object, which means that I need to directly access this object by calling plac.parser_from(main).print_help(). It would be nice to be able to say instead: plac.parser_from(get_current_function()).print_help(), so that I am not relying on the function being named “main”. Right now, my implementation of “get_current_function” would be:

import inspect    
def get_current_function():
    return eval(inspect.stack()[1][3])

But this implementation relies on the function having a name, which I suppose is not too onerous. I’m never going to do plac.call(lambda ...).

In the long run, it might be more useful to ask the author of plac to implement a print_help method to print the help text of the function that was most-recently called using plac, or something similar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:41:31+00:00Added an answer on May 18, 2026 at 9:41 pm

    The stack frame tells us what code object we’re in. If we can find a function object that refers to that code object in its __code__ attribute, we have found the function.

    Fortunately, we can ask the garbage collector which objects hold a reference to our code object, and sift through those, rather than having to traverse every active object in the Python world. There are typically only a handful of references to a code object.

    Now, functions can share code objects, and do in the case where you return a function from a function, i.e. a closure. When there’s more than one function using a given code object, we can’t tell which function it is, so we return None.

    import inspect, gc
    
    def giveupthefunc():
        frame = inspect.currentframe(1)
        code  = frame.f_code
        globs = frame.f_globals
        functype = type(lambda: 0)
        funcs = []
        for func in gc.get_referrers(code):
            if type(func) is functype:
                if getattr(func, "__code__", None) is code:
                    if funcs:
                        return None
                    funcs.append(func)
        return funcs[0] if funcs else None
    

    Some test cases:

    def foo():
        return giveupthefunc()
    
    zed = lambda: giveupthefunc()
    
    bar, foo = foo, None
    
    print bar()
    print zed()
    

    I’m not sure about the performance characteristics of this, but i think it should be fine for your use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we get variable name('PHPSESSID') defined php.ini through some function in PHP? I know
How can i get variable in handler function of obj ? Without reference of
I try to use a variable in my kshell script but can't get a
How can I get the variable My.Application.Info.Version.ToString to populate in the comments section? Dim
For some reason i can't get the variable 'total' to define at all... I
With the environment variable %allusersprofile% I can get the directory where common settings are
Using the vxWorks API symFind() we can get the address of a global variable
I can't get my head around how formatting a datetime variable inside a string
Given this variable in tcsh: set i = ~/foo/bar.c how can I get just
I can't find a correct way to get the environment variable for the appdata

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.