Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105840
| From | gvim <gvimrc@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Why lambda in loop requires default? |
| Date | 2016-03-27 02:46 +0100 |
| Message-ID | <mailman.80.1459086902.28225.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Why lambda in loop requires default? gvim <gvimrc@gmail.com> - 2016-03-27 02:46 +0100 Re: Why lambda in loop requires default? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-27 17:16 +0300 Re: Why lambda in loop requires default? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-27 18:15 +0300 Re: Why lambda in loop requires default? Ned Batchelder <ned@nedbatchelder.com> - 2016-03-27 08:29 -0700
csiph-web