Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > switching an instance variable between a property and a normal value

Reply
Thread Tools

switching an instance variable between a property and a normal value

 
 
Steven Bethard
Guest
Posts: n/a
 
      01-07-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.
 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      01-08-2005
Steven Bethard wrote:
> 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:


If you want the behaviour to be switchable per-instance, you have to go the
route of always running through the property machinery, since property() creates
a descriptor on the class, not the instance.

On the other hand, if you want to switch the behaviour of every instance, then
making usevalue and usefunc class methods may give you some mileage.

I'm rather curious about your use case, though. . .

Here's a different way of using the property machinery, too:

Py> class C(object):
.... def __init__(self):
.... self._use_val = None
.... def useval(self, x):
.... self._val = x
.... self._use_val = True
.... def usefunc(self, func, *args, **kwds):
.... self._func, self._args, self._kwds = (func, args, kwds)
.... def _get(self):
.... use_val = self._use_val
.... if use_val is None:
.... raise AttributeError('x')
.... if use_val:
.... return self._val
.... else:
.... return self._func(*self._args, **self._kwds)
.... x = property(_get)
....
Py> c = C()
Py> c.useval(4)
Py> c.x
4
Py> c.usefunc(list)
Py> c.x
[]

--
Nick Coghlan | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
including instance methods and setting an instance variable Leon Bogaert Ruby 19 03-23-2008 09:29 PM
reference variable...and normal.....variable..... dost C++ 2 04-25-2006 07:30 PM
Re: VOIP and PSTN automatic switching to your normal Telco devices Marc H.Popek VOIP 6 01-17-2005 01:43 AM
RE: switching an instance variable between a property and a normalvalue Robert Brewer Python 2 01-08-2005 06:52 AM
VOIP and PSTN automatic switching to your normal Telco devices Marc H.Popek VOIP 0 12-25-2004 08:07 PM



Advertisments