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

  • SEARCH
  • Home
  • 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 8254665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:16:10+00:00 2026-06-08T01:16:10+00:00

I want to find the name of the function as it was called …

  • 0

I want to find the name of the function as it was called … i.e. the name of the variable that called the function. Using the basic recipes i.e. with __name__, func_name, or inspecting the basic stack does not work for me. For example

def somefunc():
    print "My name is: %s" % inspect.stack()[1][3]

a = somefunc
a()
# would output: out: "My name is: somefunc"
# whereas I want it to output: "My name is: a"

My gut says I can do this, but I can’t figure it out. Any python guru’s out there?

  • 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-06-08T01:16:11+00:00Added an answer on June 8, 2026 at 1:16 am

    It’s probably impossible to do this 100% correctly, but you could give the following a try:

    import inspect
    import parser
    
    # this flatten function is by mike c fletcher
    def flatten(l, ltypes=(list, tuple)):
        ltype = type(l)
        l = list(l)
        i = 0
        while i < len(l):
            while isinstance(l[i], ltypes):
                if not l[i]:
                    l.pop(i)
                    i -= 1
                    break
                else:
                    l[i:i + 1] = l[i]
            i += 1
        return ltype(l)
    
    # function we're interested in
    def a():
        current_func = eval(inspect.stack()[0][3])
        last_frame = inspect.stack()[1]
        calling_code = last_frame[4][0]
        syntax_tree = parser.expr(calling_code)
        syntax_tree_tuple = parser.st2tuple(syntax_tree)
        flat_syntax_tree_tuple = flatten(syntax_tree_tuple)
        list_of_strings = filter(lambda s: type(s)==str,flat_syntax_tree_tuple)
        list_of_valid_strings = []
        for string in list_of_strings:
            try:
                st = parser.expr(string)
                list_of_valid_strings.append(string)
            except:
                pass
        list_of_candidates = filter(lambda s: eval(s)==current_func, list_of_valid_strings)
        print list_of_candidates
    
    # other function
    def c():
        pass
    
    a()
    b=a
    a(),b(),c()
    a(),c()
    c(),b()
    

    This will print:

    ['a']
    ['a', 'b']
    ['a', 'b']
    ['a']
    ['b']
    

    It’s pretty ugly and complicated, but might work for what you need. It works by finding all variables used in the line that called this function and comparing them to the current function.

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

Sidebar

Related Questions

I've got an IMethodMessage instance for a function that's being called. I want to
i want to find the name of a colour from the hexadecimal colour code.
i just want to find out the name of this grid to refresh it.
I want to do something on the lines of: find -name *.mk | xargs
I'm using the jquery autocomplete plugin from pengoworks: http://www.pengoworks.com/workshop/jquery/autocomplete_docs.txt In the function called upon
I'm developing a web application using .net/c#/jQuery. I want to develop an AJAX function
I'm trying to find out the name of the paramters that were passed to
I have called a function from a class to find all the items related
I want to make a function that can determine the source code of how
I want to hook functions that are called from a loaded DLL on Run

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.