switching an instance variable between a property and a normal value

Robert Brewer fumanchu at amor.org
Fri Jan 7 22:23:26 CET 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:
...
> py> c.x is c.x # I'd like this to be False

You'd like 'c.x is c.x' to be FALSE? You can't be serious. Must be a
typo.

If you want C._x to return something other than the property descriptor,
you can. Grab the sample code for Property from
http://users.rcn.com/python/download/Descriptor.htm#properties and
modify the __get__ method:

def __get__(self, obj, objtype=None):
    if obj is None:
        return self    # Return whatever you like here
    if self.fget is None:
        raise AttributeError, "unreadable attribute"
    return self.fget(obj)

That might be one way to get what you want.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list