Adding properties to an instance

dg.google.groups at thesamovar.net dg.google.groups at thesamovar.net
Wed Feb 6 18:03:16 EST 2008


On Feb 6, 11:09 pm, "bruno.desthuilli... at gmail.com"
<bruno.desthuilli... at gmail.com> wrote:
> While this is technically possible (I tried a couple years ago), it
> requires hacking the __getattribute__ method, which is something I
> would not recommand, not only because it can be tricky, but mostly
> because this is a very critical path wrt/ perfs.  (IIRC it also
> required using custom descriptors, but I'm not really sure about this
> last point).

Performance is pretty important for the class I'm designing so I think
__getattribute__ is probably out. The computed properties are only
infrequently accessed, but using __getattribute__ slows everything
down, right?

> Before new-style classes, we used the __getattr__/__setattr__ hooks
> for computed attributes. While this approach is now a bit abandonned
> in favor of descriptors (properties or custom ones), it still works
> fine, and is probably the best solution to your problem.

Ah, I didn't know about these - it looks as though they might be just
the thing since it seems they're only called after all the other
methods fail. That looks like there would be no performance hit, I
wouldn't need to mess around with dynamically changing the class, and
it would automatically deal with the (irritating) feature of having to
check if there is already something in the object's dir() with that
name. I'll look into this tomorrow - I hope this feature isn't going
to be removed in future versions of Python?

--
Dan



More information about the Python-list mailing list