Invoker of a method

A

Andre Meyer

Hi Pythoneers

In an application I am developing I want to register the invoker of a
method call. Is there any way to find out who is invoking an object's
method? I do not want to rely on the caller telling the callee his name
explicitly.

thanks
Andre
 
A

Alex Martelli

Andre Meyer said:
Hi Pythoneers

In an application I am developing I want to register the invoker of a
method call. Is there any way to find out who is invoking an object's
method? I do not want to rely on the caller telling the callee his name
explicitly.

inspect.currentframe lets you inspect the call frames up the stack,
including (of course) the caller's one; same as sys._getframe. However,
I suggest you reconsider using this in production code, as opposed to
debugging or other development-framework tasks. As the docstring says:

'''
This function should be used for internal and specialized purposes only.
'''

It's not an issue of doing what you desire with one function or another;
if you do it at all, inspect.currentframe is appropriate. Rather, the
issue is that a function _shouldn't_ go rooting around in frames up the
stack, except for debugging and other instrumentation purposes: such
"black magic" behavior is not recommended, it damages the important
quality of transparence.

Still, Python does give you plenty enough rope to shoot yourself in the
foot, as befits the principles in the "Spirit of C" which Python also
follows: trust the programmer, don't stop the programmer from doing
something that _needs_ to be done. Just make sure about that 'needs'!-)


Alex
 
A

Andre Meyer

Thanks Alex

I will try this. The need for this is that I have a sort of blackboard
architecture where multiple threads can modify information and I need to
know who is changing what and when.

g
Andre
 
A

Alex Martelli

Andre Meyer said:
Thanks Alex

You're welcome.

I will try this. The need for this is that I have a sort of blackboard
architecture where multiple threads can modify information and I need to
know who is changing what and when.

You can record the current thread without black magic, rather than
method/function names. But maybe I don't understand your architecture
well enough to offer suggestions.


Alex
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top