Why lambda in loop requires default?
gvim
gvimrc at gmail.com
Sat Mar 26 21:46:08 EDT 2016
More information about the Python-list mailing list
Sat Mar 26 21:46:08 EDT 2016
- Previous message (by thread): setup
- Next message (by thread): Why lambda in loop requires default?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message (by thread): setup
- Next message (by thread): Why lambda in loop requires default?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list