Would Anonymous Functions Help in Learning Programming/Python?

NickC ncoghlan at gmail.com
Mon Sep 24 10:59:14 EDT 2007


On Sep 24, 9:16 pm, Bruno Desthuilliers <bruno.
42.desthuilli... at wtf.websiteburo.oops.com> wrote:
> Matthew Woodcraft a écrit :
> > One reason for the different syntax is that functions, unlike most
> > other objects, know their own names (which can be shown in tracebacks
> > and the like).
>
> Nope. They know *one* of their names - the one they've been given when
> first instanciated. Which may or not be the name used to get at them...

That's exactly the point - a function may be given many names through
the assignment statement, just like any other data value. However, the
*first* name given to a function (the one in the def statement) is
special, as that is the name the function knows *itself* by.

While a function *can* be treated like any other piece of data once
you have a reference to one, the original statement does a lot more
than a normal assignment does:
  - being within the scope of a function significantly alters name
binding and lookup
  - return statements and yield statements are meaningful only within
the scope of a function
  - you can attach decorators to a function definition
  - you can include a docstring in a function definition

For the original poster, I suggest trying some of the other
suggestions in this thread, where you skip the def statement and
instead look at manipulating standard library functions like math.sin
and math.cos. Using a dictionary to select a function to run may be a
good trick to illustrate the usefulness of this technique (e.g.
passing in a command line argument to choose a function to call to
generate a test sequence)




More information about the Python-list mailing list