Current installation of the Python 3.10 is having a defect that prevents me to implement the Space Age exercise
To simplify, this is a demonstration of the defect. The same way as in the code below I wanted to add dynamically a member functions using lambdas, but this doesn’t work. I tried this in 3.10 and then, consequently on 3.9 and 3.11 - in these two defect is not repro.
list_tuples=[
('test1', 'result1'),
('test2','result2')
]
class Test:
def _my_calc(self, x):
return self.s+'_'+x
def __init__(self, s, is_bug):
self.s = s
if is_bug:
for p in list_tuples:
setattr(self, 'on_'+p[0], lambda: self._my_calc(p[1]))
else:
setattr(self, 'on_test1', lambda: self._my_calc('result1'))
setattr(self, 'on_test2', lambda: self._my_calc('result2'))
print ('This is working as expected:---------------------------')
print (Test('OK on test1:', False).on_test1())
print (Test('OK on test2:', False).on_test2())
print ('But this is not:--------------------------------------')
print (Test('OK on test2:', True).on_test2())
print (Test('WTF is wrong with test1?:', True).on_test1())
Well running it on the website so is the issue quite apparent. Lambda in that situation only takes the last value of the for loop which would be Neptune.
I would kinda recommend to avoid lambda in this situation. And instead move it.
Hey, folks. I am sorry for the confusion. It is actually consistent between 3.10.6 and 3.11.0
Late binding lambda case, it is “as expected” (not as expected by me, but this is a problem between the chair and the keyboard.) Sorry!!!