471,318 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

How can I know the name of "caller"

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

Jun 19 '07 #1
9 1539
billiejoex schrieb:
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 ;-)

Jun 19 '07 #2
...(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.
>>import traceback
def foo():
.... return traceback.extract_stack()
....
>>def bar():
.... return foo()
....
>>foo()
[('<stdin>', 1, '<module>', None), ('<stdin>', 2, 'foo', None)]
>>bar()
[('<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

Jun 19 '07 #3
On 19 Giu, 22:50, Stefan Sonnenberg-Carstens
<stefan.sonnenb...@pythonmeister.comwrote:
billiejoex schrieb:
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 ;-)- 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.

Jun 19 '07 #4
On Jun 19, 10:50 pm, Stefan Sonnenberg-Carstens
<stefan.sonnenb...@pythonmeister.comwrote:
billiejoex schrieb:
...(if it is possible) how can I get, from method "called", the name
of function/method that called it (in this case "caller")?
inspect.stack is your friend ;-)
If you start doing such things on a regular basis, it will sooner or
later become your enemy.

Gerrit.

Jun 19 '07 #5

billiejoex wrote:
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....2bb076ec4a8059

-Jay
Jun 19 '07 #6

billiejoex wrote:
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....2bb076ec4a8059

-Jay

Jun 19 '07 #7
En Tue, 19 Jun 2007 18:06:40 -0300, billiejoex <gn****@gmail.comescribió:
On 19 Giu, 22:50, Stefan Sonnenberg-Carstens
<stefan.sonnenb...@pythonmeister.comwrote:
>billiejoex schrieb:

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

inspect.stack is your friend ;-)- Nascondi 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.
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?

--
Gabriel Genellina

Jun 20 '07 #8
billiejoex wrote:
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
Jun 20 '07 #9
On 20 Giu, 06:52, John Nagle <n...@animats.comwrote:
billiejoex wrote:
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
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?

Jun 20 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by David B. Held | last post: by
42 posts views Thread by Martin Jørgensen | last post: by
21 posts views Thread by Steven T. Hatton | last post: by
13 posts views Thread by learning | last post: by
19 posts views Thread by glchin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.