[Tutor] properties and subclasses
Brian van den Broek
bvande at po-box.mcgill.ca
Tue Apr 26 16:29:48 CEST 2005
Kent Johnson said unto the world upon 2005-04-26 06:24:
> 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
Thanks, Kent.
I think I will go with the indirection way, then.
I had found the first thread you linked. I see what you mean about the
cure -- my general belief is that *I* am unlikely to have problems
for which meta-classes are really the best solution :-)
Thanks for the links and the advice.
Best to all,
Brian vdB
More information about the Tutor
mailing list