what's in a name (was Re: using lambda to print everything in a list)

Alex Martelli aleaxit at yahoo.com
Mon Apr 30 08:47:36 EDT 2001


"Roman Suzi" <rnd at onego.ru> wrote in message
news:mailman.988627062.15160.python-list at python.org...
> On Mon, 30 Apr 2001, Alex Martelli wrote:
>
> >You COULD say "the same thing", but that would not be "the
> >language capabilities don't change"; what I was specifically
> >saying is that naming "a code fragment" (to be passed as an
> >argument to some other function), rather than leaving it
> >unnamed, seems to me to result in clearer code.  So, "the
> >same thing", if said of subexpression-values, would be that
> >the naming seemed to result in clearer code.
>
> Clearer code will be the result of good commenting.
> You can add whatever lengthy comment and explain
> everything there without too much burden on the CPU.
> Calling  half a dozen functions + resolving names
> in a deeply nested loop is very inefficient.

Well-chosen names help at least as much as comments.
And the impact on efficiency is clearly anything but
intuitive:


> Probably we should ask Guido for the construction like:
>
> exec:
>   res = e**(pi*x+y)
> where:
>   x = lenthy_function()
>   y = even_larger_function()
>   pi = math.pi
>   e = math.e
>
> to has no impact on the outside name space
> by unnaming x, pi, e during byte-compilation.

Referring to math.pi over and over is a typical source
of _inefficiency_ in Python.  If math.pi doesn't change,
you're MUCH better off placing the reference in a local
variable once, outside the loop, and using that variable
inside the loop... in terms of efficiency (maybe in
terms of clarity, too -- that's more debatable).

Python's compiler does no hoisting of constant
sub-expressions, etc, so the same applies to the
results of such sub-expressions (again, in a loop).


Alex






More information about the Python-list mailing list