0
def my_function():
   ... 

my_variable = my_function
my_variable()

In this case, is there a way to get my_variable as string from inside my_function?

4
  • this code doesn't make sense. Can you explain in broader terms what you are trying to do? Commented Feb 10, 2023 at 13:17
  • I want to get the effective name of the function when it's being called. Commented Feb 10, 2023 at 13:18
  • 2
    That is not something you'd ordinarily want to do. Variable names are not data and should not influence a program's behaviour. Commented Feb 10, 2023 at 13:22
  • 1
    I'm not asking for permission or if it's right to do so, I'm asking whether it's possible. Commented Feb 10, 2023 at 13:25

1 Answer 1

1

You could look into globals() for instances of the function.

def my_func():
    names = [k for k,v in globals().items() if str(v).startswith("<function my_func ")]
    print(names[1:]) #names[0] is "my_func"

my_var = my_func
my_var() #['my_var']

asd = my_func
asd() #['my_var', 'asd']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.