[Tutor] Class Attribute "Overloading?"
Kent Johnson
kent37 at tds.net
Thu Aug 16 03:49:59 CEST 2007
Tiger12506 wrote:
> It's a little ugly but not too bad. What you are describing are properties.
>
> class Foo:
should be
class Foo(object):
> def _get_attr1(self):
> if not self.attr1:
> attr1 = TheValue
> return attr1
You have to use a different name for the actual data attribute and the
property (stack overflow, anyone?), and you didn't save the value.
if not self._attr1:
self._attr1 = TheValue
return self._attr1
> def _set_attr1(self, value):
> self.attr1 = value
> ChangeDBFunction(value)
> attr1 = property(self._get_attr1,self._setattr1,None,None)
A read-only property might be more appropriate (no _set_attr1).
>
> I think that will work. The docs seem to suggest that you should subclass
> `object`.
That is required. Properties do not work correctly with old-style
classes (not derived from object).
Kent
More information about the Tutor
mailing list