Can a function know how it has been called?

Seb

Is there a way to know within a function if the function has been called by itself or assigned to a variable with = ?

I would like to do something like this:

def func():
    if 'assigned with equal':
        return 5
    else:
        print 'not assigned'

that would give those outputs:

func()
-> 'not assigned'
a = func()
a
-> 5
Darrick Herwehe

Yes, there is a way to do this, though getting it right will be tricky. You can use the inspect module to access the call stack. This allows you to see what the code looks like that called the function.

The stack looks something like this:

[(<frame object at 0x107de1d38>, 'path/to/function/file.py', 6, 'func', ['\tstack = inspect.stack()\n'], 0), (<frame object at 0x107d34050>, 'path/to/calling/file.py', 17, '<module>', ['func()\n'], 0)]

Notice the second to last entry: ['func()\n']. This is showing the code that calls your function. Even though the name of the function shows up elsewhere in the stack, it always shows the actual name of the function no matter how it is called. So you have to do a little work on your own to determine whether or not the call was made directly or through an assigned variable.

This is the only way to get the function name. Python does not have a feature to retrieve the function name from within the function itself.

To make it clear that this will be more difficult than just if func_name in stack, I've fleshed out the function a little bit to show a couple of real world examples of how a function might be called and what it would look like. Because the function can't determine its own name, it is hardcoded at the top of the function.

import inspect


def func(var=None, default=''):
    my_name = 'func'
    stack = inspect.stack()
    func_call = stack[-1][4][0]
    print func_call.rstrip()  # `func_call` contains a trailing newline

    # logic to determine if the name matches
    # ...
    # ...


x = func
func()
return_value = x(var=1)
print func()
if x():
    pass

This prints:

func()
return_value = x(var=1)
print func()
None  # This prints from the call, because `func()` doesn't return anything
if x():

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I know from where a function has been invoked/called?

How can I test that a function has not been called?

Is there a way for the MainViewController to know when a press IBAction function has been called?

How can I know that onCreateView has been called from an outer class?

How to check if a function has been called in Rust?

How to determine if a function has been called in Powershell

How to know when Parse.initialize() has already been called?

How to know if initscr() of ncurses has been called earlier?

How do I know the location a process has been called

How can I repeat functions in F# after a function has been called inside it?

How can I know how long the application has been used?

How to test how many times a function has been called

React, how to know when a pure function component has been rendered?

How can I know if a branch has been already merged into master?

Enzyme/Jest: How to test if mocked function has been called

Identify how the function has been called in closure javascript

Python: How to wait until a function has been called in a different thread?

Scheme: How to check if a function has been called with a same argument

How can I tell if another object method has been called

How can I test if a callback closure has been called?

Android/Java: Is there a way to know how often an external method has been called?

How do I know if add_subparsers() has already been called on a parser

How do I know if my Python script has been called with the interpreter on the command line or as an executable (via shebang)?

How can I test if function have not been called?

Delay function execution if it has been called recently

How would you implement a javascript function so that it has a notion of how many times it has been called?

how to know if a button has been clicked in javascript?

How to know if a method has been defined in an interface

How to know what Linklabel has been clicked?