Implementing class attribute access methods via pseudo-functi on o verloading.

Alex Martelli aleaxit at yahoo.com
Tue Oct 26 10:24:21 EDT 2004


<Doran_Dermot at emc.com> wrote:

> Just for the hell of it I'll stick my neck out and say that my method
> implementations still look easier for a newbie Python programmer to read
> since there are fewer Python idioms involved.

Of course: if you do things in a way that's very far from a normal
Python approach, you won't use normal Python idioms (but, rather, poor
imitations of other languages' idioms).  No surprise there.

BTW, since you top-post (another practice that's quite far from the
usual courtesy of Pythonistas) -- my code that you're _presumably_
commenting on (hard do say, when you post a solid block of your text
followed by a verbatim copy of a post; normal courteous practice is very
different, as you can easily tell by just _reading_ for a while in this
newsgroup for example), namely...:

> If you want to accomodate Java crazies (with setFoo/getFoo manias,
> universal there) you might...:
> 
> >>> class x(object):
> ...   def __getattr__(self, name):
> ...     if name.startswith('get') and name[3:4].isupper():
> ...        n = name[3:4].lower() + name[4:]
> ...        return lambda: getattr(self, n)
> ...     raise AttributeError, name

...is meant as a _joke_ (though it's working Python code), as the use of
the words 'crazies' and 'manias' indicated.  Only a crazy person would
rather use 'print x.getFoo()' than a normal 'print x.foo' to the same
effect, is the implication -- though Python allows you to set up
automatic translation from the crazy form to the normal form in _FIVE_
lines that a bright beginner should have no more trouble following than
for any other mild application of metaprogramming/inspection (indicating
an amount of power which _I_, at least, still find pretty impressive).

The only sensible practice is using properties and educating misguided
newbies who think methodcalling is preferable (much less the _inferior_
C++ fashion of overloading getter and setter method with the same name,
which I strongly advised against even when I _was_ a C++ mentor &c...).


Alex



More information about the Python-list mailing list