Dynamic use of property() fails
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Tue Apr 15 09:01:19 EDT 2008
Hrvoje Niksic a écrit :
> Bruno Desthuilliers <bruno.42.desthuilliers at websiteburo.invalid>
> writes:
>
>>> However, if you know what you're doing, you can simply customize your
>>> class's __getattribute__ to do what *you* want for your objects.
>> <op>
>> But bear in mind that, beside possible unwanted side-effectn, you'll
>> get a non-negligible performance hit - __getattribute__ being, as the
>> name implies, invoked on each and every attribute lookup.
>
> That's unavoidable, though -- whatever you do to customize your class
> in Python, the result will be slower than the C code built into
> Python.
This one customization hook is probably the most sensible still.
> Fortunately, not every object is performance-critical. In
> this case, __getattribute__ buys you per-instance customization not
> otherwise available. The code you posted is probably more efficient,
> but at the cost of losing the ability to customize specific instances
> of the class.
You could as well just customize __setattr__/__getattr__ (which is what
was done before new-style classes and descriptors). But my own
experience is that the very few times I thought I had a need for
per-instance descriptors, I found other solutions that were certainly
easier to grasp and maintain on the long term. Not that I would not be
able to deal with per-instance descriptors, but so far the cost
outweighted the benefits IMHO. Now YMMV of course !-)
More information about the Python-list
mailing list