How can I know the name of "caller"

B

billiejoex

Hi there,
unfortunately, I'm compelled to apply a sort of monkey patching at the
code of an existing libreary that I can't modify directly.
Here's my question
Having such code:

class test:

def caller(self):
self.b()

def called(self):
pass

....(if it is possible) how can I get, from method "called", the name
of function/method that called it (in this case "caller")?

Thanks in advance
 
S

Stefan Sonnenberg-Carstens

billiejoex said:
Hi there,
unfortunately, I'm compelled to apply a sort of monkey patching at the
code of an existing libreary that I can't modify directly.
Here's my question
Having such code:

class test:

def caller(self):
self.b()

def called(self):
pass

...(if it is possible) how can I get, from method "called", the name
of function/method that called it (in this case "caller")?

Thanks in advance
inspect.stack is your friend ;-)
 
Z

zacherates

...(if it is possible) how can I get, from method "called", the name
of function/method that called it (in this case "caller")?

The traceback module will give you access to the call stack.
.... return traceback.extract_stack()
........ return foo()
....
[('<stdin>', 1, '<module>', None), ('<stdin>', 2, 'bar', None),
('<stdin>', 2, 'foo', None)]


However, this seems like one of those it-matters-where-you-call-it-
from functions from days of yore. They're generally considered to be
a bad idea and was one of the patterns that prompted a rather famous
essay by Dijkstra.

I'd recommend exploring other design alternatives and leave this
monstrosity in peace.

Cheers,
Aaron
 
B

billiejoex

billiejoex schrieb:









inspect.stack is your friend ;-)- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

Thank you man. That's what I was searching for.
This should be production code. Is insepct.stack fast enough?
Considering that I'd have to use inspect.stack inside a 'while'
statement looping different times, I wouldn't slow down my application.
 
J

Jay Loden

billiejoex said:
Hi there,
unfortunately, I'm compelled to apply a sort of monkey patching at the
code of an existing libreary that I can't modify directly.
Here's my question
Having such code:

class test:

def caller(self):
self.b()

def called(self):
pass

...(if it is possible) how can I get, from method "called", the name
of function/method that called it (in this case "caller")?

Thanks in advance

I asked a similar/related question recently, it might put you on the right track also:

http://groups.google.com/group/comp...076ec4a8059?lnk=st&q=&rnum=2#ce2bb076ec4a8059

-Jay
 
J

Jay Loden

billiejoex said:
Hi there,
unfortunately, I'm compelled to apply a sort of monkey patching at the
code of an existing libreary that I can't modify directly.
Here's my question
Having such code:

class test:

def caller(self):
self.b()

def called(self):
pass

...(if it is possible) how can I get, from method "called", the name
of function/method that called it (in this case "caller")?

Thanks in advance

I asked a similar/related question recently, it might put you on the right track also:

http://groups.google.com/group/comp...076ec4a8059?lnk=st&q=&rnum=2#ce2bb076ec4a8059

-Jay
 
G

Gabriel Genellina

Thank you man. That's what I was searching for.
This should be production code. Is insepct.stack fast enough?
Considering that I'd have to use inspect.stack inside a 'while'
statement looping different times, I wouldn't slow down my application.

A faster way is to use sys._getframe(1).f_code.co_name
But it doesn't feel good for production code... can't you find a different
approach?
 
J

John Nagle

billiejoex said:
Hi there,
unfortunately, I'm compelled to apply a sort of monkey patching at the
code of an existing libreary that I can't modify directly. ....

...(if it is possible) how can I get, from method "called", the name
of function/method that called it (in this case "caller")?

Bad idea.

Note, though, that within Python, you can easily replace existing
function definitions with your own. This is something of a desperation
measure, but it works.

I have a small collection of patches to the standard Python libraries
which I import. (I've reported all of them in the tracker as bugs, and some
later version of Python will contain the fixes. But that can take years.)

John Nagle
 
B

billiejoex

Bad idea.

Note, though, that within Python, you can easily replace existing
function definitions with your own. This is something of a desperation
measure, but it works.

I have a small collection of patches to the standard Python libraries
which I import. (I've reported all of them in the tracker as bugs, and some
later version of Python will contain the fixes. But that can take years.)

John Nagle

Yeah, it seems really horrible to me too. Python is awesome but
stdlib, imo, lacks of properly maintenance.
I sincerely don't know what to do...
In a production environment what could be better? Overriding the
bugged method or including the patched version of the entire module
into the distribution?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top