Op 27-03-16 om 03:46 schreef gvim:
> Given that Python, like Ruby, is an object-oriented language why doesn't this:
It has nothing to do with being object-oriented but by how scopes are used
> def m():
> a = []
> for i in range(3): a.append(lambda: i)
> return a
Python doesn't create a
On 3/26/2016 9:46 PM, gvim wrote:
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
def echo_i: return i
b = m()
for n in range(3): print(b[n]()) # => 2 2 2
) # => 2 2 2
...
On Sunday, March 27, 2016 at 9:55:16 AM UTC-4, g vim wrote:
> Given that Python, like Ruby, is an object-oriented language
It turns out that "object-oriented" means very little, and lots
of languages that are object-oriented will behave differently
from each other, even where object behavior is
gvim writes:
> 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 = []
gvim writes:
> 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
I'm going to suggest two variations that may or may not
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