def multiply_list():
return [lambda x: i * x for i in range(4)]
print([m(100) for m in multiply_list()])
for m in multiply_list():
print(m(100))
def multiply_gene():
return (lambda x: i * x for i in range(4))
print([m(100) for m in multiply_gene()])
for m in multiply_list():
print(m(100))
def multiply_yield():
for i in range(4):
yield lambda x: x * i
print([m(100) for m in multiply_yield()])
for m in multiply_yield():
print(m(100))
from functools import partial
from operator import __mul__
def multiply():
return [partial(__mul__, i) for i in range(4)]
print([m(100) for m in multiply()])
for m in multiply():
print(m(100))
标签:闭包,closure,list,yield,print,range,现象,multiply,100
From: https://blog.51cto.com/u_16055028/6505252