41
votes
5answers
5k views

What do (lambda) function closures capture in Python?

Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code: adders= [0,1,2,3] for i in [0,1,2,3]: adders[i]=lambda a: ...
199
votes
22answers
83k views

Python Lambda - why?

I'm just beginning Python and ran head first into Lambda- which took me a while to figure out. Is lambda one of those 'interesting' language items that in real life should be forgotten? I'm sure ...
51
votes
5answers
28k views

List filtering: list comprehension vs. lambda + filter

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items. My code looked like this: my_list = [i for i in my_list if i.attribute == ...
5
votes
2answers
878 views

Python closure not working as expected

When I run the following script, both lambda's run os.startfile() on the same file -- junk.txt. I would expect each lambda to use the value "f" was set to when the lambda was created. Is there a way ...
20
votes
8answers
46k views

Finding the average of a list

I have to find the average of a list in Python. This is my code so far l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce(lambda x, y: x + y, l) I've got it so it adds together the values in the ...
54
votes
4answers
12k views

No Multiline Lambda in Python: Why not?

I've heard it said that multiline lambdas can't be added in Python because they would clash syntactically with the other syntax constructs in Python. I was thinking about this on the bus today and ...
23
votes
10answers
12k views

Which is more preferable to use in Python: lambda functions or nested functions ('def')?

I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found ...
20
votes
7answers
7k views

Can a lambda function call itself recursively in Python?

A regular function can contain a call to itself in its definition, no problem. I can't figure out how to do it with a lambda function though for the simple reason that the lambda function has no name ...
8
votes
4answers
4k views

Syntax behind sorted(key=lambda :)

I don't quite understand the syntax behind the sorted() argument: key=lambda variable: variable[0] Isn't lambda arbitrary? Why is variable stated twice in what looks like a dict?
7
votes
3answers
3k views

What is “lambda binding” in Python?

I understand what are lambda functions in Python, but I can't find what is the meaning of "lambda binding" by searching the Python docs. A link to read about it would be great. A trivial explained ...
3
votes
5answers
224 views

lambda function don't closure the parameter in Python?

Code talks more: from pprint import pprint li = [] for i in range(5): li.append(lambda : pprint(i)) for k in li: k() yield: 4 4 4 4 4 why not 0 1 2 3 4 ?? Thanks. P.S. ...
2
votes
3answers
381 views

Use value of variable in lambda expression

a = [] a.append(lambda x:x**0) a.append(lambda x:x**1) a[0](2), a[1](2), a[2](2)... spits out 1, 2, 4, ... b=[] for i in range(4) b.append(lambda x:x**i) b[0](2), b[1](2), b[2](2)... spits out ...
0
votes
1answer
54 views

strange behavior with lamba: getattr(obj, x) inside a list [duplicate]

In the following example: class A(object): pass prop1 = 1 prop2 = 2 prop3 = 3 prop4 = 4 obj = A() tmp = ['prop1', 'prop2', 'prop3', 'prop4'] getter = [ lambda: getattr(obj, x) ...
9
votes
5answers
1k views

Python Lambda Problems

What's going on here? I'm trying to create a list of functions: def f(a,b): return a*b funcs = [] for i in range(0,10): funcs.append(lambda x:f(i,x)) This isn't doing what I expect. I ...
4
votes
3answers
235 views

Python lambdas and scoping

Given this snippet of code: funcs = [] for x in range(3): funcs.append(lambda: x) print [f() for f in funcs] I would expect it to print [0, 1, 2], but instead it prints [2, 2, 2]. Is there ...

1 2 3
15 30 50 per page