confused with lambda~
sismex01 at hebmex.com
sismex01 at hebmex.com
Tue Sep 24 09:49:27 EDT 2002
> that describe in reference documentation is too short,
> I couldnt even understand what it trying to say.
> any explanation please ?
Lambdas are complicated to understand, but it's easier
if you think of them as a single-expression inline
function (not inline in the code-generation sense
of the word, but of the definition sense).
See, "def" is a statement, and defines a word in the
local namespace to be a function, and "lambda" returns
a reference to a function. Kinda hard to understand :-)
So, the equivalence would be something like this:
Using def:
>>> def square(a): return a*a
Using lambda:
>>> square = lambda a:a*a
lambda's syntax is "lambda <params>:<expression>", quite
simple really, but what makes them hard is the concept
of an "unbound" function. Another example:
Using def:
>>> def SumOverProduct(a,b): return (a+b)/(a*b)
Using lambda:
>>> SumOverProduct = lambda a,b: (a+b)/(a*b)
You may use anything Python in the argument list, including
the *args and **kw forms. One thing you have to look out
for is the form where local variables are propagated to
the lambda's namespace (it's a function, remember?) using
default arguments:
def some_func(...):
... code ...
L = lambda a, b=SomeLocalVar, c=Another: operations(a, b, c)
... more code ...
In previous versions of Python, there was no way to access
variables outside the lambda's namespace unless it was
by propagating them via default arguments, in more recent
versions it's possible to do away with this practice,
if you know what you're doing :-)
Well... what more can I say. It's a complex subject,
I really recommend you practice them if you want to use
them, until you get the hang of using them to make your
programs more clear.
Notice: USE them, don't ABUSE them; they either make a
program much more clearer, or they make them a program
hideously obtuse and obscure, and difficult to read
and understand (we all know who the culprits might be
so I won't say names). ;-)
Good luck, practice, play around with them, and then
store them somewhere safe where they won't do much
harm. :-)
-gus
Advertencia:
La informacion contenida en este mensaje es confidencial y restringida y
esta destinada unicamente para el uso de la persona arriba indicada, Esta
comunicacion representa la opinion personal del remitente y no refleja
necesariamente la opinion de la Compañia. Se le notifica que esta
estrictamente prohibida cualquier difusion, distribucion o copia de este
mensaje. Si ha recibido esta comunicacion o copia de este mensaje por error,
o si hay problemas en la transmision, favor de comunicarse con el remitente.
Todo el correo electrónico enviado para o desde esta dirección será
procesado por el sistema de correo corporativo de HEB. Tal correo
electrónico esta sujeto a ser almacenado y puede ser revisado por alguien
ajeno al recipiente autorizado con el propósito de monitorear que se cumplan
las normas de seguridad de la empresa.
More information about the Python-list
mailing list