code_junkie

How do I create a list of Python lambdas (in a list comprehension/for loop)

Nov 13th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. listOfNumbers = [1,2,3,4,5]
  2. square = lambda x: x * x
  3. listOfLambdas = [lambda: square(i) for i in listOfNumbers]
  4.  
  5. for f in listOfLambdas:
  6. print f(),
  7.  
  8. 1 4 9 16 25
  9.  
  10. 25 25 25 25 25
  11.  
  12. listOfLambdas = []
  13. for num in listOfNumbers:
  14. action = lambda: square(num)
  15. listOfLambdas.append(action)
  16. print action()
  17.  
  18. for f in listOfLambdas:
  19. print f(),
  20.  
  21. def square(x): return lambda : x*x
  22. listOfLambdas = [square(i) for i in [1,2,3,4,5]]
  23. for f in listOfLambdas: print f()
  24.  
  25. 1
  26. 4
  27. 9
  28. 16
  29. 25
  30.  
  31. listOfLambdas = [lambda: i*i for i in range(6)]
  32.  
  33. for f in listOfLambdas:
  34. print f()
  35.  
  36. 25
  37. 25
  38. 25
  39. 25
  40. 25
  41. 25
  42.  
  43. listOfLambdas = [lambda i=i: i*i for i in range(6)]
  44.  
  45. for f in listOfLambdas:
  46. print f()
  47.  
  48. 0
  49. 1
  50. 4
  51. 9
  52. 16
  53. 25
  54.  
  55. listOfLambdas = [lambda i=i: square(i) for i in listOfNumbers]
  56.  
  57. listOfLambdas = map(lambda i: lambda: square(i), listOfNumbers)
  58.  
  59. for num in range(1, 6):
  60. pass
  61. assert num == 5 # num is now bound in the enclosing scope
  62.  
  63. for num in range(1, 6):
  64. spam = 12
  65. assert num == 5 # num is now bound in the enclosing scope
  66. assert spam == 12 # spam is also bound in the enclosing scope
  67.  
  68. [num for num in range(1, 6)]
  69. assert num == 5
  70.  
  71. functions = []
  72. for number in range(1, 6):
  73. def fun():
  74. return number
  75. functions.append(fun)
  76. assert all(fun() == 5 for fun in functions)
  77. assert all(fun() is number for fun in functions)
  78.  
  79. # Same as above -- commented out for emphasis.
  80. #functions = []
  81. #for number in range(1, 6):
  82. # def fun():
  83. # return number
  84. # functions.append(fun)
  85. #assert all(fun() == 5 for fun in functions)
  86. #assert all(fun() is number for fun in functions)
  87. number = 6 # Rebind 6 in the scope and see how it affects the results.
  88. assert all(fun() == 6 for fun in functions)
  89.  
  90. def get_fun(value):
  91. """:return: A function that returns :param:`value`."""
  92. def fun(): # Bound to get_fun's scope
  93. return value
  94. return fun
  95.  
  96. functions = []
  97. for number in range(1, 6):
  98. functions.append(get_fun(number))
  99. assert [fun() for fun in functions] == range(1, 6)
  100.  
  101. # Same as before, commented out for emphasis.
  102. #functions = []
  103. #for number in range(1, 6):
  104. # functions.append(get_fun(number))
  105. #assert [fun() for fun in functions] == range(1, 6)
  106. print functions[0].__closure__
  107. # Produces: (<cell at 0x8dc30: int object at 0x1004188>,)
  108.  
  109. >>> class square(object):
  110. ... def __init__(self, val):
  111. ... self.val = val
  112. ... def __call__(self):
  113. ... return self.val * self.val
  114. ...
  115. >>> l = [1,2,3,4,5]
  116. >>> funcs = [square(i) for i in l]
  117. >>> for f in funcs:
  118. ... print f()
  119. ...
  120. 1
  121. 4
  122. 9
  123. 16
  124. 25
  125. >>>
Add Comment
Please, Sign In to add comment