Given that Python, like Ruby, is an object-oriented language why doesn't this:

def m():
  a = []
  for i in range(3): a.append(lambda: i)
  return a

b = m()
for n in range(3): print(b[n]())  # =>  2  2  2

... work the same as this in Ruby:

def m
  a = []
  (0..2).each {|i| a << ->(){i}}
  a
end

aa = m
(0..2).each {|n| puts aa[n].()}  # =>  0  1  2


lambda i=i: i

... is needed to make it work in Python. Just wondered why?

gvim
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to