Question on lambdas

Steven D'Aprano steve at pearwood.info
Tue Dec 9 01:38:02 EST 2014


On Tue, 09 Dec 2014 02:44:12 +0100, Christoph Becker wrote:

> Ben Finney wrote:
> 
>> It's best to remember that ‘lambda’ is syntactic sugar for creating a
>> function; the things it creates are not special in any way, they are
>> normal functions, not “lambdas”.
> 
> Could you please elaborate why ‘lambda’ does not create “lambdas”.  I'm
> a Python beginner (not new to programming, though), and rather confused
> about your statement.


Compare these:

py> def f1():
...     return 23
... 
py> f2 = lambda: 23
py> type(f1) is type(f2)
True


Functions created with lambda are exactly the same type of object as 
functions created with def.

Functions created with lambda accept exactly the same arguments as 
functions created with def. They differences are:

- functions created with lambda all have the same internal name;
- functions created with def have the name you give them;

- functions created with lambda are expressions and can be embedded
  directly in function calls, lists, etc;
- functions created with def are statements and must stand alone.




-- 
Steven



More information about the Python-list mailing list