explain this function to me, lambda confusion

andrej.panjkov at climatechange.qld.gov.au andrej.panjkov at climatechange.qld.gov.au
Wed May 7 20:34:56 EDT 2008


On May 8, 7:38 am, globalrev <skanem... at yahoo.se> wrote:
> i have a rough understanding of lambda but so far only have found use
> for it once(in tkinter when passing lambda as an argument i could
> circumvent some tricky stuff).
> what is the point of the following function?
>
> def addn(n):
>                 return lambda x,inc=n: x+inc
>
> if i do addn(5) it returns
>
> >>> def addn(n):
>
>                 return lambda x,inc=n: x+inc
>
> >>> addn(5)
>
> <function <lambda> at 0x01D81830>
>
> ok? so what do i write to make it actually do something. and is the
> inc=n necessary i cant do x+n?

Here are some notes I have written for our local wiki on lambdas in
python.  I hope you will find them illuminating, and I would welcome
any suggestions for improving them.

I have just cut and pasted from our wiki, so the fancy formatting has
been lost.

-----

Python lambdas.

The on-line documentation for python lambdas is not very illuminating.
Here’s my take and my first simple examples.

I would describe a lambda as a parameterised function template. If you
dig, the docs call lambdas anonymous functions not bound to a name.
There is a bit of resemblance to C macros.

Here is a simple lambda that implements an exclusive or:

>>> def XOR(x,y) :
>>>   return lambda : ( ( x ) and not ( y ) ) or ( not ( x ) and ( y ) )

(Because of the resemblance to C macros, I have been cautious and
written the lambda with lots of parentheses.)

To use this in later code, we define instances of the lambda with
specific function arguments.

>>> topping = XOR( cream, icecream)
>>> sauce = XOR( tomato, BBQ )


We now have two “functions”, topping() and sauce() which we can use
later to test flags.

>>> cream = True
>>> icecream = False
>>> print topping()
True


So in the definition of the XOR lambda, think of x and y as the
parameters of the function template, and XOR as the function name
placeholder.

By putting in specific objects for the parameters (here the boolean
variables cream and icecream for example), we produce a specific
instance of the lambda, topping() which looks like a function with no
arguments.

If we use different objects for the parameters (say tomato and BBQ)
then we get a different function, sauce.

Here is another simple lambda, (template) to set up three new
functions AddOnly, DeleteOnly, and ReplaceOnly.

#--# Lambda function to check that a flag is only on when the other
two are off. #--#
def TFF(x,y,z) :
  return lambda : ( ( x ) and not ( y ) and not ( z ) )

AddOnly = TFF( options.AddAction, options.ReplaceAction,
options.DeleteAction )
DeleteOnly = TFF( options.DeleteAction, options.AddAction,
options.ReplaceAction )
ReplaceOnly = TFF( options.ReplaceAction, options.AddAction,
options.DeleteAction )

if( not (DeleteOnly() or AddOnly() or ReplaceOnly() ) ):
  print "Error:  Exactly one of  [ --add | --replace | --delete ]
allowed. "
  parser.print_help()
  exit


More advanced lambdas.

The examples above give function instances that have no arguments,
once the parameters of the lambda are chosen.

For a function template with arguments and parameters, we add the
arguments on the 2nd line. Parameters are in the first line.

The Gaussian distribution is exp(-(x-μ)²/ 2σ² ) / √(4 πσ). While we
can think of this as a function of three variables, we normally view
it as a family of functions of a single variable x, parameterised by μ
and σ. Selecting fixed values for μ and σ gives us a single
distribution for x.

>>> import math
>>> def Gaussian( mu, sigma ) :
...   return lambda x : math.exp( - (x-mu)**2 / 2 /sigma**2 ) /
math.sqrt (2 * math.pi *sigma **2 )
...
>>>

and here are some instances:

>>> Normal = Gaussian (0, 1)
>>> HeightDistrib = (170, 20)

which we later use as

>>> y = Normal( 0.5 )
0.35206532676429952
>>> x = 192
>>> HeightDistrib(x)
0.0073381331586869951

I recommend defining the instances of the lambda right after the
lambda. If you define it in code far removed from the definition of
the lambda, it looks like an assignment, so comment it.



More information about the Python-list mailing list