Named code blockes

Rainer Deyke root at rainerdeyke.com
Tue Apr 24 14:19:22 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9c45qg01sui at news1.newsguy.com...
> "Rainer Deyke" <root at rainerdeyke.com> wrote in message
> > In Python, unnamed objects have an additional benefit: there is a single
> > consistent way of naming them.  Consider:
> >
> > a = 5
> > b = lambda: None
> > def c():
> >   pass
> >
> > These are three assignments, but only two look like assingments.  The
>
> They are three ways to bind (or re-bind) names, but only two of them
> ARE "assignments" -- Python's syntax defines assignments, plain and
> augmented, rather precisely, and there is no 'def' keyword there:-).

I count four ways ('import', 'def', '=', and augmented assignment), not
counting 'globals().set' and similar tricks.

> > special syntax for the third case does not make it clear to newbies that
> 'c'
> > is a reference like any other which happens to refer to a function
object.
>
> And you think newbies will find the definition for b any clearer?!

No, because the keyword 'lambda' has no intrinsic meaning.  Lambda syntax in
its current form is problematic, but that doens't mean the idea behind it
is.  Some obvious readability improvements:

1. Change the keyword.

b = lambda: None  -->  b = function: None

2. Place parenthesis around the argument list.

b = function: None --> b = function(): None

Of course unnamed functions will remain crippled as long as their body is
limited to a single expression, and embedding a statement suite in an
expression in Python is problematic because of indentation issues.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list