Name: Anonymous 2017-02-28 4:18
Does it?
def foo(a, methods=[]): # methods defaults to [] if not specified
for i in range(3):
# Append an anonymous function, which returns `x + i`
# when given `x`
methods.append(lambda x: x + i)
sum_methods = 0
# For each function `method` of in `methods`, sum `method(a)`
for method in methods:
sum_methods += method(a)
return sum_methods
You expect your function to output the following:foo(0) = (0 + 0) + (0 + 1) + (0 + 2) = 3foo(1) = (1 + 0) + (1 + 1) + (1 + 2) = 6
foo(2) = (2 + 0) + (2 + 1) + (2 + 2) = 9
foo(2, [lambda x: x]) = ((2)) + ((2 + 0) + (2 + 1) + (2 + 2)) = 11
Simple, right? Let’s try it in our terminal.6foo(0)18foo(1)36foo(2)14foo(2, [lambda x: x])
Well, there seems to be a minor error. But wait, could we have missed something? Let’s check again.24foo(0)45foo(1)72foo(2)
WHAT.
THE.