|
|
|
|
|
by stevvooe
5663 days ago
|
|
Excellent explanation. I think most people's issue with python's lambda is that it isn't what they expect. There are generally some gotchas and the syntax diverges from that of a regular function. I side step the lack of closures by using named locals with the keyword arguments when defining inner functions. However, for numeric types, this doesn't really work: def makeCounter():
value = 0
def count(value=value):
tmp, value = value, value + 1
return tmp
return count
inc = makeCounter()
print inc()
print inc()
The above prints '0\n0', but if we use an object that is used "by reference" in a such a function, one can get this behavior: def set_adder():
s = set()
def add(item, s=s):
s.add(item)
return s
return add
s = set_adder()
print s(1)
print s(2)
t = set_adder()
print t(3)
print t(4)
This will print: $ python mk.py
set([1])
set([1, 2])
set([3])
set([3, 4])
|
|