Properties fun with 2.2

Mike C. Fletcher mcfletch at rogers.com
Mon Dec 31 20:11:44 EST 2001


The problem with that is, what if I want properties 'a' and '_a'?  That
is, if superclass X stores the value of "a" as "_a" in the object's
slots, (if I understand correctly), then any assignment to _a (in a
sub-class or not) will overwrite the value of a.  When you store the
data in the dict under the same name (a), then you only have the 1 name
taken up in the namespace, there's no surprises, just a documented
property accessed as if it were a regular attribute.

That becomes a significant question when you're using "automagic"
properties like those with which I started the thread (ones that don't
know what their property names are until instantiated (and don't know
the final conditions of the class), and which manage all of the data
storage automagically).

Simple and clean is the reason properties exist in my little version of
the universe :) .

Enjoy,
Mike

Martin von Loewis wrote:

...

 > Delegating to a different attribute might be appropriate:
 >
 > class X(object):
 >     __slots__ = ['_a']
 >     def set_a(self, val):
 >         self._a = val
 >     def get_a(self):
 >         return self._a
 >     a = property(get_a, set_a)
 >
 > Regards,
 > Martin

_______________________________________
    Mike C. Fletcher
    http://members.rogers.com/mcfletch/







More information about the Python-list mailing list