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?
#1
how can a function find the name by which it is called?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You could try inspect.stack()[1][4][0] and see if that is useful.
Reply
#3
import sys


def who_called_me():
    print(sys._getframe().f_back.f_code.co_name)


def i_am_the_caller():
    who_called_me()


i_am_the_caller()
Output:
i_am_the_caller
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Depends on what is wanted, but that seems to display the name of the function, not the name that calls it. (similar to inspect.currentframe().f_code.co_name) If you assign the function to a variable and call the variable, it still returns the original function name.
Reply
#5
if foo calls bar() the name i want is "bar", not "foo"

if foo does xyzzy = bar then calls xyzzy() the name i want is "xyzzy"
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Aug-14-2022, 07:05 PM)Skaperen Wrote: if foo does xyzzy = bar then calls xyzzy() the name i want is "xyzzy"
If foo does
xyz  = [bar, bar, bar]
what do you want when foo calls
xyz[0]()
? Also what is the purpose of all this?
Reply
#7
(Aug-14-2022, 07:28 PM)Gribouillis Wrote: what do you want when foo calls
xyz[0]()
probably "xyz[0]". if i can get that piece of code, that would work. i suspect otherwise i'd get the function's def name.

(Aug-14-2022, 07:28 PM)Gribouillis Wrote: Also what is the purpose of all this?
to allow the function to output a more meaningful message in the case of certain errors.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Aug-14-2022, 07:05 PM)Skaperen Wrote: if foo calls bar() the name i want is "bar", not "foo"

if foo does xyzzy = bar then calls xyzzy() the name i want is "xyzzy"

That's exactly what the 'inspect.stack()' line I posted above should do. Did you try it?
Reply
#9
(Aug-14-2022, 10:57 PM)Skaperen Wrote: to allow the function to output a more meaningful message in the case of certain errors.
Then why don't you output the whole stack traceback, or simply raise an exception? The traceback is the most precise error report.
ibreeden, ndc85430, buran like this post
Reply
#10
i am wanting to phrase the message in a certain context for this user base that is following some specific instructions to get specific results. these users are running commands (in the future, to use a GUI app) that constructs some Python code. these users are not coders and probably have no understanding of a traceback or the Python message. if the function can get the name that would simplify the task. otherwise i will need to add another argument in both the function and all makers of calling code.
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 509 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Couldn't install a go-game called dlgo Nomamesse 14 3,205 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  Error in find pearson correlation function erneelgupta 1 1,896 Mar-01-2022, 03:41 PM
Last Post: stevendaprano
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,189 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  pdfminer package: can't find exgtract_text function Pavel_47 7 5,323 Jan-25-2021, 03:31 PM
Last Post: Pavel_47
  What is this formatting called? Mark17 2 1,792 Dec-14-2020, 08:42 PM
Last Post: snippsat
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,253 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  How do I find if a function has been defined? AndyHolyer 3 2,295 Jul-24-2020, 01:39 PM
Last Post: Gribouillis
  Class Instances called in the wrong order IanIous 4 2,875 Mar-06-2020, 02:16 PM
Last Post: IanIous
  How to find a zero of this function? kkitti93 4 3,793 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