A critic of Guido's blog on Python's lambda

Petr Prikryl prikryl at skil.cz
Wed May 10 02:09:26 EDT 2006


"Alex Martelli" wrote...
> Joe Marshall  wrote:
>    ...
> > If you language allows unnamed integers, unnamed strings, unnamed
> > characters, unnamed arrays or aggregates, unnamed floats, unnamed
> > expressions, unnamed statements, unnamed argument lists, etc.  why
> > *require* a name for trivial functions?

Event the trivial function can have a name. Does it make
the trivial function more understandable if I do not give a name?
I understand what lambda means, but it is clearer for me to see
something like (with more meaningful name than shown here):

    >>> def fn(x):
    ...     return x + 5

and to use it like...

    >>> fn(3)
    8

Still, I can anonymize it later, if I really need

    >>> a = [fn]

or I can give it another name

   >>> z = fn

In my opinion, the cry for lambda in Python comes
from people used to functional languages, because
they are not used to the procedural style and it
does not fit them at the beginning.
Frankly, functional style seems "more strange" to me
(i.e. the counter example). Still, I do not say it is bad.
But it never came to my mind to say "Please, rewrite
the LISP so that it looked more like Pascal. I do not like
the lambda."

Also, Python is compiled (to bytecode), imperative language,
and the form of definition of the chunks of code is more
natural to the compiler and to the customs
how the procedural program is written and how its parts
are referenced and put together to form bigger chunks
of code.

> I think it's reasonable to make a name a part of functions, classes and
> modules because they may often be involved in tracebacks (in case of
> uncaught errors): to me, it makes sense to let an error-diagnosing
> tracebacks display packages, modules, classes and functions/methods
> involved in the chain of calls leading to the point of error _by name_.

I agree with Alex here. I USUALLY do not need debugger, but sometimes
it is exremely handy to discover what happens (through debugger or few
print commands, its a matter of taste). And Python is very fine here:

    >>> dir(fn)
    ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__',
    '__getattribute__', '__hash__', '__init__', '__module__', '__name__',
    '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
    '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict',
    'func_doc', 'func_globals', 'func_name']
    >>> fn.__name__
    'fn'

and even the anonymized function still knows its original name and type

    >>> a[0].__name__
    'fn'
    >>> type(a[0])
    <type 'function'>

If lambda functions in Python are tweaked internally into normal Python
functions...

    >>> z = lambda x: x * 3
    >>> dir(z)
    ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__',
    '__getattribute__', '__hash__', '__init__', '__module__', '__name__',
    '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
    '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict',
    'func_doc', 'func_globals', 'func_name']
    >>> z.__name__
    '<lambda>'
    >>> type(z)
    <type 'function'>

... what makes them better, than the named function? If I do not loose
anything by simplification, I want to have it simpler.

pepr





More information about the Python-list mailing list