Python Forum
how can a function find the name by which it is called?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can a function find the name by which it is called?
#11
(Aug-15-2022, 11:38 PM)Skaperen Wrote: these users are not coders and probably have no understanding of a traceback
I had the same problem once with a drawing language that I created for non programming users. The solution that I used was a very strong arguments checking for the commands that those users could call. They virtually could not call the commands with wrong arguments, so that the commands could not fail. This strategy has some limitations too.
Reply
#12
(Aug-14-2022, 11:43 PM)bowlofred Wrote: That's exactly what the 'inspect.stack()' line I posted above should do. Did you try it?
i don't understand how to use it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
(Aug-16-2022, 03:18 PM)Gribouillis Wrote: They virtually could not call the commands with wrong arguments
if the user specifies it. the code maker (makes Python3 code) uses that name instead of trying to figure out what the user really meant. the user can get things wrong.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
You just call it from within the function that wants to know the way it was called.

import inspect
def foo():
    print("hello")
    print(f"Called as {inspect.stack()[1][4][0]}")

bar = foo

foo()
bar()
Skaperen and ibreeden like this post
Reply
#15
There is some code that I wrote a long time ago here: https://python-forum.io/thread-3347.html...call+stack
which follows the execution path from a given point.
Here, i believe group[0].function will contain the function name of the calling method

It's been a while, but I think that is correct.
Reply
#16
@Skaperen

I've just been reading this...

https://python-course.eu/advanced-python...ration.php

... which put me in mind of this thread. I'm not too sure if the information from the above link can be applied to what you have in mind, but it maybe it's a step in the right direction.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#17
(Aug-14-2022, 12:59 AM)bowlofred Wrote: You could try inspect.stack()[1][4][0] and see if that is useful.
i tried it like this:
Output:
lt1a/forums/1 /home/forums 12> cat func_name.py def foo(*args,**kwargs): import inspect me = inspect.stack()[1][4][0] print('i was called as',repr(me),flush=True) return me bar = foo print(f'woot {foo()},{bar(456)}',flush=True) lt1a/forums/1 /home/forums 13> python3.8 func_name.py i was called as "print(f'woot {foo()},{bar(456)}',flush=True)\n" i was called as "print(f'woot {foo()},{bar(456)}',flush=True)\n" woot print(f'woot {foo()},{bar(456)}',flush=True) ,print(f'woot {foo()},{bar(456)}',flush=True) lt1a/forums/1 /home/forums 14>
but that was not what i wanted. i only want the name. with this i would need to parse the line (not too hard) and determine which name this call was (much harder).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#18
Skaperen Wrote:with this i would need to parse the line (not too hard) and determine which name this call was (much harder).
You can walk a line with the ast module to find all the expressions that were called in the line
>>> line = "print(f'woot {foo()},{bar(456)}',flush=True)\n"
>>> import ast
>>> p = ast.parse(line)
>>> for node in ast.walk(p):
...     if isinstance(node, ast.Call):
...         print(ast.unparse(node.func))
... 
print
foo
bar
other example
>>> line = "xyz[0](spam, bar.x[1].egg(3))"
>>> p = ast.parse(line)
>>> for node in ast.walk(p):
...     if isinstance(node, ast.Call):
...         print(ast.unparse(node.func))
... 
xyz[0]
bar.x[1].egg
Note: ast.unparse() needs Python >= 3.9. For earlier pythons, there is an «astunparse» module in Pypi.
Reply
#19
short answer: the solution is complex.

since this project design is not yet selected, the ideas that need to have this function discover how it got called will be ruled out. hopefully a simpler design for my project can be worked out.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple variable inputs when only one is called for ChrisDall 2 488 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Couldn't install a go-game called dlgo Nomamesse 14 3,090 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  Error in find pearson correlation function erneelgupta 1 1,859 Mar-01-2022, 03:41 PM
Last Post: stevendaprano
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,108 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  pdfminer package: can't find exgtract_text function Pavel_47 7 5,254 Jan-25-2021, 03:31 PM
Last Post: Pavel_47
  What is this formatting called? Mark17 2 1,768 Dec-14-2020, 08:42 PM
Last Post: snippsat
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,206 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  How do I find if a function has been defined? AndyHolyer 3 2,261 Jul-24-2020, 01:39 PM
Last Post: Gribouillis
  Class Instances called in the wrong order IanIous 4 2,819 Mar-06-2020, 02:16 PM
Last Post: IanIous
  How to find a zero of this function? kkitti93 4 3,753 Jan-16-2020, 08:44 AM
Last Post: kkitti93

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020