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

Doran_Dermot at Doran_Dermot at
Tue Oct 26 15:27:08 CEST 2004

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.

Now that should solicit a few comments ;-)

Thanks to all who have helped me out on this question.  The whole discussion
(albeit a bit one sided) has been both fun and informative.




I'm using Python because I don't think Java is that good!  Please go easy on
the "blue fire"!

-----Original Message-----
From: at
[ at]On Behalf Of
aleaxit at
Sent: 26 October 2004 13:17
To: python-list at
Subject: Re: Implementing class attribute access methods via
pseudo-functi on o verloading.

<Doran_Dermot at> wrote:
> It's interesting to note that you prefer code like: theProblem.title =
> Title"
> However, I wonder if people coming from the Java/C++ world prefer:
> theProblem.title( "The Title" )
> Obviously the first version is less typing! However, the second version
> might feel more natural to Java/C++ programmers.  Just an observation!

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

and whatever else.  Now, you can read any attribute of instances of x
with that silly syntax to your heart's contents.  E.g.:

c = x() = 23
print c.getFoo()

There!  The ULTIMATE Java-Python bridge.  Now, at last, nothing will
impede Python from vying for the hearts and minds of Javaites.  (BTW: it
works just fine with properties, too).

The extension of this idea to methods such as setFoo is left as an
exercise to the reader, as is the trivial optimization of caching the
lambda into self, just in case it's repeatedly called.


More information about the Python-list mailing list