It looks like homework, so I’m not just giving you the answers. Here are two functions you can perform and see how the values change.
def make_constants_like_generator():
def make_constant(x):
def inner(y):
return x
return inner
results = []
for i in [1, 2, 3, 4]:
results.append(make_constant(i))
for f in results:
print f(None)
return results
def make_constants_like_list():
x = None
results = []
for i in [1, 2, 3, 4]:
x = i
def inner(y)
return x
results.append(inner)
for f in results:
print f(None)
return results
Lazy assessment waits until the last possible moment to evaluate the expression. The opposite is an impatient assessment. The generator expression is lazy, it does nothing until it is repeated. The list expression is impatient, as soon as it occurs, the list is filled with values.
, , . python . , , , , , ,
def map(func, iter):
return (func(val) for val in iter)