Named code blockes

Alex Martelli aleaxit at yahoo.com
Tue Apr 24 11:24:02 EDT 2001


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:0P5F6.85546$J%5.30205213 at news2.rdc2.tx.home.com...
> Alex Martelli writes:
> >
> > > Unnamed blocks are a very powerful programming device.  As far as I
can
> >
> > But where's the extra power in making them UNNAMED?  It's
> > so easy to name them, after all.
>
> Leaving things unnamed can make code a lot more readable (and writable).

Leaving SOME things unnamed can, while others are better off for
being named.  Using APPROPRIATE names is particularly, well,
appropriate:-).

> Consider:
>
> def f(a, b, c):
>   return (-b + (b*b - 4*a*c) ** 0.5) / (2 * a)

Introducing one naming:
    discriminant = b*b - 4*a*c
may be a good idea here -- because that name is often
used in the published literature related to this formula,
and helps simplify the complicated expression in ways
that will feel most natural to readers.

Now, the question refers specifically to 'blocks' of
executable code, not to "naming" in general (a very
interesting issue, to be sure, both in programming
and in other contexts).


> 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:-).

> 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?!

Maybe the Python newbies I'm dealing with have very different
background from the ones you know -- I find it hard to explain
this perceptual difference otherwise.


Alex






More information about the Python-list mailing list