properties and get/set methods
Mike C. Fletcher
mcfletch at rogers.com
Sun Apr 6 14:41:51 EDT 2003
Terry Reedy wrote:
>"Mike C. Fletcher" <mcfletch at rogers.com> wrote in message
>news:mailman.1049604615.22077.python-list at python.org...
>
>
>>In these cases, you're using properties to simplify the modelling of
>>domain objects, or to provide some *generic* functionality over and
>>above a regular value get/set. You don't want to have to specify
>>
>>
>that
>
>
>>functionality for every property, you just want to say "this is one
>>
>>
>of
>
>
>>_those_ properties". The code bloat (not to mention mindless
>>duplication of code) when you get up to 15 or 20 properties for an
>>object would be ridiculous.
>>
>>
>
>Perhaps what you want are __get/setattr__ methods that check if the
>attribute is one of 'those-properties' (in a set/dict lookup) and if
>so, does the generically different thing. My impression is that
>properties are meant for doing attribute specific customization
>without loading up/slowing down __get/setattr__ with a long list of
>if/elif blocks.
>
>Terry
>
>
The annoyance there is that you then have to have every object aware of
every property-type you want to support. You either wind up with a
"super property-holding class" from which every object must be derived,
and which is aware of all of the property types, or you do holder-class
mix-ins, or you have some sort of plug-in system to lookup the
appropriate function based on the property type... sounds a lot like
what you get with having the property's type (i.e. class) store the
appropriate method to begin with.
When you get dozens of property-types, and individual properties need to
store data about themselves (e.g. their constraints, their data-types)
which requires another out-of-band storage mechanism if you're not
storing them in the properties themselves, the use of an external
__getattribute__/__setattr__ mechanism starts to fall down pretty fast.
Shrug :) .
However, it works very well for such things as the original poster was
describing (i.e. a single (or small number of) generic thing(s) to be
done for a large fraction of properties of an object). You can see a
nice example in the Zope3 persistence module.
Enjoy all,
Mike
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list