Python becoming less Lisp-like

Paul Boddie paul at boddie.org.uk
Thu Mar 17 03:57:15 EST 2005


"Kay Schluehr" <kay.schluehr at gmx.net> wrote in message news:<1110999333.028296.319800 at l41g2000cwc.googlegroups.com>...
> 
> Some people refused properties in Python for exactly this reason.
> Defining somewhat like:
> 
> def _get_X(self):
>     return self._X
> 
> def _set_X(self,X):
>     self._X =3D X
> 
> X =3D property(_get_X, _set_X )
> 
> in a Java-style fashion is indeed awfull and clumsy and that people
> dismiss such boilerplate code is understandable.

The principal advantage of the property function was to permit the
definition of "active" attributes without having a huge
"if...elif...else" statement in the __getattr__ method. So the
motivation was seemingly to externalize the usually simple logic in
__getattr__ so that one could know a bit more about the supported
properties of an object without having to read the source code.

Java should be able to support Python-style properties by converting
property accesses to accessor method calls before runtime, but with
this capability within easy reach (and in all irony), the creators of
the language instead decided to force programmers to explicitly call
these rigidly-named methods, despite the raw property names actually
being exposed to external tools like property editors, JavaServer
Pages, and so on.

[accessor code cut]

> class X(object):
>     a = accessor("a")
>     b = accessor("b", types = (tuple,list))
>     c = accessor("c", check = lambda x:hasattr(x,"__len__"))

Hmmm, lambda!

> a,b,c are indeed properties of X !
> 
> >>> x = X()
> >>> x.b = [1,2,3] # o.k
> >>> x.b
> [1, 2, 3]
> >>> x.b = 0  # raises a TypeError, because a tuple/list is expected

This is a nice example, but I do wonder why so many examples of
Python's dynamicity, especially with many of the newer features, seek
to provide static type checking or some other variant on that theme.
It would be nice to see more work done at the level of the compiler to
achieve those kinds of goals.

  BRIDGEKEEPER:
    Stop!
    Who would cross the Bridge of Death must answer me these
    questions three, ere the other side he see.
  THE PYTHONIC KNIGHT:
    Ask me the questions, bridgekeeper. I am not afraid.
  BRIDGEKEEPER:
    What... is your name?
  THE PYTHONIC KNIGHT:
    My name is 'The Pythonic Knight'.
  BRIDGEKEEPER:
    What... is your quest?
  THE PYTHONIC KNIGHT:
    To seek the Holy Grail.
  BRIDGEKEEPER:
    What... are decorators for?
  THE PYTHONIC KNIGHT:
    Static type checking. No, param-- auuuuuuuugh!

Or something [1] like that.

Paul

[1] http://www.mwscomp.com/movies/grail/grail-23.htm



More information about the Python-list mailing list