Skip to main content
Indentations are usually four spaces/one tab.
Source Link
Torxed
  • 23.6k
  • 15
  • 91
  • 135

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it)

funcs = []
for i in range(4):
    def f():
        print(i)
    funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it)

funcs = []
for i in range(4):
  def f():
    print(i)
  funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it)

funcs = []
for i in range(4):
    def f():
        print(i)
    funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp

pretty sure that line was intended to be inside the loop
Source Link
TigerhawkT3
  • 49.5k
  • 6
  • 66
  • 101

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it)

funcs = []
for i in range(4):
  def f():
    print(i)
  funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it)

funcs = []
for i in range(4):
  def f():
    print(i)
funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it)

funcs = []
for i in range(4):
  def f():
    print(i)
  funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp

deleted 82 characters in body
Source Link
Luca
  • 1.8k
  • 6
  • 22
  • 45

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it):

funcs = []
for i in range(4):
    def f():
        print(i)
    funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp, maybe I'll become clear with further reading, but can you please spoil it for me?

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it):

for i in range(4):
    def f():
        print(i)
    funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp, maybe I'll become clear with further reading, but can you please spoil it for me?

I'm a fairly experienced C/C++ (and to some degree, Java) programmer. I'm learning python, but I'm baffled at some strange (for my backgroung) behaviors of the language.

I'm learning about nested function and closures (reading "Learning Python", that seems a really good source for me).

I understand that if I nest a def inside a for loop when I call the created function, it looks up the last value of the captured loop variable (as it captures by reference, as a C++ programmer would put it)

funcs = []
for i in range(4):
  def f():
    print(i)
funcs.append(f)

and running the program the result is

>>> for f in funcs:
      f()


3
3
3
3

Now, I was wrapping my head around this when I stumbled upon this (what to me seems) an inconsistency: if I do

for i in range(4):
  funcs[i]()


0
1
2
3

more baffling, if I do

>>> i = 2
>>> funcs[i]()

2

and now, all functions in list returns 2:

for f in funcs:
  f()


2
2
2
2

there must be some scope related question that I can't grasp

added 12 characters in body
Source Link
Torxed
  • 23.6k
  • 15
  • 91
  • 135
Loading
Source Link
Luca
  • 1.8k
  • 6
  • 22
  • 45
Loading