473,396 Members | 2,014 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,396 software developers and data experts.

Invoker of a method

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

--
Dr. Andre P. Meyer http://home.hccnet.nl/a.meyer/
TNO FEL Command & Control and Simulation, http://www.fel.tno.nl/div2/
Delft Cooperation on Intelligent Systems, http://www.decis.nl/
Jul 18 '05 #1
3 1612
Andre Meyer <me***@acm.org> wrote:
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
Jul 18 '05 #2
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
Alex Martelli wrote:
Andre Meyer <me***@acm.org> wrote:

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

Jul 18 '05 #3
Andre Meyer <me***@acm.org> wrote:
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
7
by: greenflame | last post by:
I am trying to make a matrix object. I have given it some properites. I am trying to add a method. When I call the method by Test.showDims(...) I want to only enter one input, that is the method by...
1
by: Dan | last post by:
hi ng, i want to give the invoker of my application information whether application has passed successfully. it shall get scheduled, and if the exeution did not pass, it should be repeated some...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
1
by: Troels Arvin | last post by:
Hello, In PostgreSQL, when you create a function, you may choose between SECURITY INVOKER or SECURITY DEFINER: http://www.postgresql.org/docs/8.2/static/sql-createfunction.html When a function...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.