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. 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
Thanks Bengt for your answer, it's really what I needed !
Regards,
Popov This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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....
|
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);...
|
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.
|
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...
|
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...
|
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.
...
|
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.
...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| | |