switching an instance variable between a property and a normal value
Steven Bethard
steven.bethard at gmail.com
Fri Jan 7 15:12:58 EST 2005
I'd like to be able to have an instance variable that can sometimes be
accessed as a property, and sometimes as a regular value, e.g. something
like:
py> class C(object):
... def usevalue(self, x):
... self.x = x
... def usefunc(self, func, *args, **kwds):
... self._func, self._args, self._kwds = func, args, kwds
... self.x = C._x
... def _get(self):
... return self._func(*self._args, **self._kwds)
... _x = property(_get)
...
py> c = C()
py> c.usevalue(4)
py> c.x
4
py> c.usefunc(list)
py> c.x # I'd like this to print []
<property object at 0x04166DA0>
Of course, the code above doesn't do what I want because C._x is a
property object, so the assignment to self.x in usefunc just adds
another name for that property object. If I use self._x (or
getattr(self, '_x'), etc.) then self._func only gets called that one time:
py> class C(object):
... def usevalue(self, x):
... self.x = x
... def usefunc(self, func, *args, **kwds):
... self._func, self._args, self._kwds = func, args, kwds
... self.x = self._x
... def _get(self):
... return self._func(*self._args, **self._kwds)
... _x = property(_get)
...
py> c = C()
py> c.usefunc(list)
py> c.x is c.x # I'd like this to be False
True
Is there any way to get the kind of behavior I'm looking for? That is,
is there any way to make self.x use the property magic only some of the
time?
Steve
P.S. Yes, I know I could make both access paths run through the
property magic, with code that looks something like:
py> class C(object):
... _undefined = object
... def __init__(self):
... self._value, self._func = C._undefined, C._undefined
... def usevalue(self, x):
... self._value = x
... self._func = C._undefined
... def usefunc(self, func, *args, **kwds):
... self._func, self._args, self._kwds = func, args, kwds
... self._value = C._undefined
... def _get(self):
... if self._value is not C._undefined:
... return self._value
... if self._func is not C._undefined:
... return self._func(*self._args, **self._kwds)
... raise AttributeError('x')
... x = property(_get)
...
py> c = C()
py> c.usevalue(4)
py> c.x
4
py> c.usefunc(list)
py> c.x is c.x
False
This code is kinda complicated though because I have to make sure that
only one of self._func and self._value is defined at any given time.
More information about the Python-list
mailing list