newbie: good reasons for learning Python?

Moshe Zadka m at moshez.org
Wed Jun 25 08:16:08 EDT 2003


On Wed, 25 Jun 2003, "Fredrik Lundh" <fredrik at pythonware.com> wrote:

> > This is a bit longer but should be more "pythonic" and far easier to
> > understand:
> 
> well, you're still using the locals() hack.

It's possible to be very evil and yet not use locals():

for n in range(99,0,-1):print(lambda _=lambda n:"%s bottle"%{0:"no"}.get(n,n)+
"s"*(n!=1)+" of beer":"%(c)s%(w)s, %(c)s,\ntake one down, pass it around,\n%(b"
")s%(w)s."%dict(b=_(n-1),c=_(n),w=" on the wall"))()

(Requires Python2.3)

Note how I obey coding conventions like "do not put more than 80 characters
on a line". Also note how in this example, indentation and line-endings
are irrelevant: delete them all, put more in the middle, randomly indent --
it'll still work :) Only thing you can't do arbitrarily is add a newline
between the : and print(...

Explanation of trick: Anyone who knows scheme will recognize my usage of
something very similar to the "let" macro to work around the fact that
assignments in Python are statements.
-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/





More information about the Python-list mailing list