Skip to content

Instantly share code, notes, and snippets.

@kvbik
Last active August 29, 2015 14:04
Show Gist options
  • Save kvbik/305f284cee31a2c2cbf3 to your computer and use it in GitHub Desktop.
Save kvbik/305f284cee31a2c2cbf3 to your computer and use it in GitHub Desktop.
co je na tom spatne?
In [1]: class O(object):
...: def _neco(self, param):
...: return param
...: def __init__(self, *args, **kwargs):
...: self.something = s = {}
...: for a,b in [(1,10),(2,20),(3,30)]:
...: s[a] = lambda: self._neco(b)
...:
In [2]: o = O()
In [3]: o.something[1]()
Out[3]: 30
functions = {}
def f(param):
return param
for a,b in [(1,10), (2,20), (3,30)]:
functions[a] = lambda: f(b)
# ocekval bych 10, ale dostanu 30
print functions[1]()
# ocekval bych 10, ale dostanu 30
print functions[2]()
# to je ta posledni lambda, takze dostanu 30
print functions[3]()
@kvbik
Copy link
Author

kvbik commented Aug 2, 2014

ani to nemusi byt objekt.. viz lambda2.py

@whiskybar
Copy link

b = 17
print functions[1]()  #hadej

b = 42
print functions[1]()  #hadej znovu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment