classes (was Re: Same again please for OOP)
aleaxit at yahoo.com
Tue Dec 26 01:08:55 CET 2000
"Moshe Zadka" <moshez at zadka.site.co.il> wrote in message
news:mailman.977739488.28224.python-list at python.org...
> I've learned the hard way not to write __getattr__ methods. Currently,
> of my code contains them. True gurus, like Gordon McMillan, can use
> (e.g., in filecmp.py), to get lazy evaluation of attributes, but I tend to
> fear those things.
I'm no guru (not a _Python_ one, I mean; at COM and C++, I'm somewhat
more experienced) yet I've happily used __getattr__ for some lazy evaluation
tasks since quite early on. Actually, __getattr__ (for lazy evaluation, as
opposed to implicit delegation, which is another cool use) tends to be a
simple thingy that delegates attribute foo to __get_foo__, attribute bar
to __get_bar__, and so on, and comes from a mixin.
> The "computed attributes" interface needs to be improved befroe Python can
> be said to be not one of *those* languages.
I wouldn't mind getting the __get_foo__ delegation automatically (though
using Boost Python gets me that, I don't _always_ have it around... yet:-),
and I'd love a simpler and more explicit way to express implicit and
automatic delegation of certain attributes to some sub-object, but, with
some naming-conventions and some luck, I haven't been bitten yet (not
in production code -- halfway decent unit tests would show up any silly
error in this area in a prototype, I should hope).
I dearly hope the special-casing of stuff now handled from __getattr__
doesn't get out of hand -- with complex structures and whatever. I
think 'officializing' the specific-attribute getter/setter naming convention
might solve 80% of the cases at little conceptual cost, and something
like a sequence of (directory, delegate) pairs somewhat parallel to the
current __class__.__bases__ (but, per-object, and with 'filtered'
delegation, rather than 'delegate whatever the delegate can handle')
might add a further 10+%, relegating 'bare __getattr__' to very rare
needs indeed. I wouldn't mind as a Python user, I'd rejoice as a
teacher of Python to beginners:-).
But __getattr__ ain't too bad... with suitable mixins, I can have even
beginners using it quite productively without real risks of accidents!-)
(__setattr__, now, THAT is admittedly something else again!-)
More information about the Python-list