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

Doran_Dermot at emc.com Doran_Dermot at emc.com
Tue Oct 26 09:27:08 EDT 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.

Cheers!!

Dermot

P.S.

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

-----Original Message-----
From: python-list-bounces+doran_dermot=emc.com at python.org
[mailto:python-list-bounces+doran_dermot=emc.com at python.org]On Behalf Of
aleaxit at yahoo.com
Sent: 26 October 2004 13:17
To: python-list at python.org
Subject: Re: Implementing class attribute access methods via
pseudo-functi on o verloading.


<Doran_Dermot at emc.com> wrote:
   ...
> It's interesting to note that you prefer code like: theProblem.title =
"The
> 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()
c.foo = 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.


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list