473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1646
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
7
by: runsun pan | last post by:
I wanna check if an object is the *arguments* object of a function, is that possible? When we do: typeof(arguments) it always returns "object", doesn't seem to be helpful.
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
0
by: devito | last post by:
hi there, for some days i try to build the boost.python tutorial "hello world" without bjam on winxp by using mingw. so i wrote a *.bat-file like the following: // --- snip...
19
by: glchin | last post by:
Does a compiler guarantee that the variable w below is placed on an eight-byte aligned address? void myFunction( long iFreq ) { const double w = two_pi * iFreq; ... ... }
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.