[Tutor] properties and subclasses
Kent Johnson
kent37 at tds.net
Tue Apr 26 12:24:42 CEST 2005
Brian van den Broek wrote:
> Hi all,
>
> I'm trying to get a hang of properties. It isn't quite clear to me
> what is the best way to make properties differ in subclasses. Some
> code snips to show what I've tried:
>
> I can get what I want this way:
>
>>>> class A(object):
>
> ... def __init__(self): pass
> ... def prop_set(self): return "I was set by A's method"
> ... my_property = property(prop_set)
> ...
>
>>>> class AA(A):
>
> ... def __init__(self): pass
> ... def prop_set(self): return "I was set by AA's method"
> ... my_property = property(prop_set)
> ...
>
>>>> aa = AA()
>>>> aa.my_property
>
> "I was set by AA's method"
>
> But the reduplication of the prop_set definition and the my_property
> assignment line seem to go against the grain of OOP to me.
That is the simplest way to do it. Your indirection approach is also valid. These two threads have
some discussion and other solutions but IMO the cures are worse than the disease:
http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/8c96e856c8966c67/6594dfecff4a3744?q=subclass+property&rnum=6&hl=en#6594dfecff4a3744
http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/95b9b8df2fa0ad3/a4e8896bd9074f23?q=subclass+property&rnum=2&hl=en#a4e8896bd9074f23
Kent
More information about the Tutor
mailing list