[Tutor] calling setters of superclasses
Alan Gauld
alan.gauld at btinternet.com
Mon Dec 20 17:23:27 CET 2010
"Steven D'Aprano" <steve at pearwood.info> wrote
>> I don't use properties in Python very often (hardly ever in fact)
>> and I've never used @setter so there may be naming requirements I'm
>> not aware of. But in general I'd avoid having two methods with the
>> same name.
>
> That's generally good advice, since one will over-write the other,
> but in this specific case, the following is completely bad:
Yep, that's what I suspected might be the case.
> If you don't use the same name, chaos reigns:
>
> >>> class Broken(object):
> ... def __init__(self):
> ... @property
> ... def x(self):
> ... @x.setter
> ... def set_x(self, value):
> ...
> >>> obj = Broken()
> >>> obj.x = 20
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: can't set attribute
All part of the reason I don't particularly like properties in Python.
It feels like it goes against the principle of explicit is better than
implicit... That and the fact that properties encourage a
data-centric view of objects.
Alan G.
More information about the Tutor
mailing list