473,382 Members | 1,639 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,382 software developers and data experts.

In a function, how to get the caller object ?

Look at this code:

class C:
def func():
<caller_of_func>.my_attrib = 1
func = staticmethod(func)

C.func()

c = C()
c.func()

In func, I would like to get C for the first call, and c for the
second one, to be able to add a new attribute to this object (so that
would add the attribute either to the class object or to the
instance). How can I do this ?
I tried to give a look to the inspect module with no luck.
Jul 18 '05 #1
2 4752
On 3 Dec 2003 06:56:06 -0800, go****@evpopov.com (popov) wrote:
Look at this code:

class C:
def func():
<caller_of_func>.my_attrib = 1
func = staticmethod(func)

C.func()

c = C()
c.func()

In func, I would like to get C for the first call, and c for the
second one, to be able to add a new attribute to this object (so that
would add the attribute either to the class object or to the
instance). How can I do this ?
I tried to give a look to the inspect module with no luck.


You need something other than staticmethod, that delivers an ordinary method or
an effective classmethod depending on access via the class or an instance, e.g.,
(not tested beyond what you see):
(BTW, I added a value parameter to your func, for less ambiguous example results)
class SpecialMethod(object): ... def __get__(self, obj, cls):
... if obj is None: return self.fun.__get__(cls, cls)
... else: return self.fun.__get__(obj, type(obj))
... def __init__(self, fun):
... self.fun = fun
... class C(object): ... def func(caller_of_func, value=1):
... caller_of_func.my_attrib = value
... func = SpecialMethod(func)
... C <class '__main__.C'> C.func <bound method C.func of <class '__main__.C'>> vars(C) <dictproxy object at 0x008F8E30> vars(C).keys() ['__dict__', '__module__', '__weakref__', '__doc__', 'func']

The above shows not my_attrib
C.func()
vars(C).keys() ['__module__', 'my_attrib', 'func', '__dict__', '__weakref__', '__doc__']

But after C.func() it is there
C.my_attrib 1

now we make an instance
c=C()
c.func <bound method C.func of <__main__.C object at 0x00904070>>

looks like an ordinary bound method (though defined with "caller_of_func" in place of "self").
c.func('another value')
c.my_attrib 'another value' C.my_attrib 1

The instance attribute shadows the class attribute, so we can uncover the latter
by deleting the former:
del c.my_attrib
c.my_attrib

1

That's really C.my_attrib found through standard attribute search.

To get more info, read about descriptors.

Regards,
Bengt Richter
Jul 18 '05 #2
Thanks Bengt for your answer, it's really what I needed !

Regards,

Popov
Jul 18 '05 #3

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
2
by: Mark | last post by:
The situtation is that I'm trying to ensure that certain functions are only called by functions that I want them to be called from. I have a popup window which has a function which calls a function...
1
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
4
by: Thomas Mlynarczyk | last post by:
Hi, I stumbled over a strange behaviour of Mozilla. When I want to access the caller property of a function that was not called from within another function, Mozilla seems to abort the script....
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
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.
3
by: william | last post by:
My situation is here: an array of two dimension can only be defined locally within a function(because the caller don't know the exact size ). Then the question is: how should the caller access...
7
by: =?UTF-8?B?QW50w7NuaW8gTWFycXVlcw==?= | last post by:
Hi, Sorry if this's been discussed before, I couldn't find it. As well you know, the ECMAScript standard doesn't include any way to access a function's caller. This has been available on Mozilla...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
9
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?

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.