What is n in the for loop that begins: "for n in sequence:"
Question:
I’m wondering what the technical description is of the variable n
in a for
loop that begins for n in sequence
. I suppose my question is about nomenclature. Would it be accurate to say that it’s a temporary named variable that gets discarded upon conclusion of the loop? I suspect that there’s a canonical way of referring to it. Thanks!
Answers:
In the example:
for n in [1,2,3]:
print(n)
print(n)
The output would be:
1
2
3
3
I.e. n
is a regular variable that is created in the for...
line. It takes the value of each item in the sequence as you go through the loop.
And after the for loop is completed, the n
variable remains with the last value that it was assigned (i.e. the last value in the sequence unless you break out of the for loop early)
It’s an ordinary variable, which happens to be the target of an assignment. What assignment? The assignment of a value produced by the __next__
method of the iterator being traversed by the loop.
Every for
loop of the form
for <var> in <iterable>:
...
could be transformed into an equivalent while
loop.
_itr = iter(<iterable>)
while True:
try:
<var> = next(_itr) # Here's the assignment
except StopIteration:
break
...
del _itr
<var>
is not discarded after the loop completes. (The temporary variable _itr
is, but it’s truly an implementation detail of the for
loop, not something that was ever visible to the Python code.)
Like any other assignment, <var>
is defined the first time it is assigned to, and it lives until the end of the scope where it was defined or until it is removed with a del
statement).
I’m wondering what the technical description is of the variable n
in a for
loop that begins for n in sequence
. I suppose my question is about nomenclature. Would it be accurate to say that it’s a temporary named variable that gets discarded upon conclusion of the loop? I suspect that there’s a canonical way of referring to it. Thanks!
In the example:
for n in [1,2,3]:
print(n)
print(n)
The output would be:
1
2
3
3
I.e. n
is a regular variable that is created in the for...
line. It takes the value of each item in the sequence as you go through the loop.
And after the for loop is completed, the n
variable remains with the last value that it was assigned (i.e. the last value in the sequence unless you break out of the for loop early)
It’s an ordinary variable, which happens to be the target of an assignment. What assignment? The assignment of a value produced by the __next__
method of the iterator being traversed by the loop.
Every for
loop of the form
for <var> in <iterable>:
...
could be transformed into an equivalent while
loop.
_itr = iter(<iterable>)
while True:
try:
<var> = next(_itr) # Here's the assignment
except StopIteration:
break
...
del _itr
<var>
is not discarded after the loop completes. (The temporary variable _itr
is, but it’s truly an implementation detail of the for
loop, not something that was ever visible to the Python code.)
Like any other assignment, <var>
is defined the first time it is assigned to, and it lives until the end of the scope where it was defined or until it is removed with a del
statement).