Skip to content

Instantly share code, notes, and snippets.

@maizy
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maizy/9345787 to your computer and use it in GitHub Desktop.
Save maizy/9345787 to your computer and use it in GitHub Desktop.
выполнить, а затем разобраться почему так происходит
# coding: utf-8
# задача:
# создать список сумматоров (функций одного параметра x), возвращающих:
# x + 10 + i
# где i от 1 до 4
# lambda
rlambda = []
for i in range(5):
rlambda.append(lambda x: x + i + 10)
# def
rdef = []
for j in range(5):
def _ap(x):
return x + j + 10
rdef.append(_ap)
# def + builder
rdef_builder = []
def mk_ap(j):
def _ap(x):
return x + j + 10
return _ap
for j in range(5):
rdef_builder.append(mk_ap(j))
# tests
print("rlambda")
print((rlambda[0])(5)) # 0 + 5 + 10 = 15 ?
print((rlambda[1])(5)) # 1 + 5 + 10 = 16 ?
print("")
print("rdef")
print((rdef[0])(5))
print((rdef[1])(5))
print("")
print("rdef + builder")
print((rdef_builder[0])(5))
print((rdef_builder[1])(5))
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment