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?
#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


Messages In This Thread
RE: how can a function find the name by which it is called? - by Skaperen - Aug-21-2022, 06:56 PM

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