Random thoughts

6 thoughts
last posted Aug. 17, 2014, 7:45 p.m.

2 earlier thoughts

0

Here's another one :

f1 = lambda : 1
f2 = lambda : 2

closure = lambda *x: x
foo, bar = closure(f1, f2)
f2 = lambda : 3
baz, = closure(f2)

# another way to do above
closure2 = lambda *x: x if len(x)>1 else x[0]
baz = closure2(f2)

f1 = lambda : 4
f2 = lambda : 5

print foo(), bar(), baz() # 1 2 3

Interesting how lambdas can make closures.

This also works with regular functions (def); It's the parameters that become lexically bound.

3 later thoughts