3

If I had the code...

list = ['Clemont', 'Albert', 'Shiro']
for x in range(len(list)):
    print(x)

...what would technical term for x be here? I'm assuming it's "iterate object" but maybe that's a misconception; if it's not a misconception, what is an iterate object?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Anya
  • 395
  • 2
  • 13

4 Answers4

10

The formal name for x in that context is target, as illustrated by Python's language reference:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

...with target_list (and target) defined as:

target_list     ::=  target ("," target)* [","]
target          ::=  identifier
                     | "(" target_list ")"
                     | "[" target_list "]"
                     | attributeref
                     | subscription
                     | slicing
                     | "*" target

If you'd like to know what a target can actually accept or be defined as, feel encouraged to peruse the documentation. By most standard meanings, you're using some kind of simple identifier, like a variable name (in this case, x) to represent the target.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 1
    But the term `target` is used for the LHS in [any old assignment](https://docs.python.org/3.3/reference/simple_stmts.html#assignment-statements), it's not specific to a `for` loop. – tobias_k Mar 16 '17 at 21:21
  • @tobias_k: That's right. It's not specific to anything necessarily. That *implies* that there's no special name for it, from the context of the grammar. – Makoto Mar 16 '17 at 21:22
  • 1
    I disagree; this only means that the same syntax can be used for both. There might be a more specific term that can be used when discussing with others to make them understand what you are talking about. – tobias_k Mar 16 '17 at 21:23
  • What you've quoted is the grammar specification. `target_list` and `target` are grammatical elements, which are common to many different types of statements. All this is saying is that the syntax for a loop variable is the same as the syntax for the destination of an assignment. – Barmar Mar 16 '17 at 21:55
3

"Loop variable" is as technical as you can get.

There's no official name since officially it isn't any different than the Left Hand Side for assignment statments; same rules apply to the "loop" variables as they do for the assignment targets.

Calling it "loop variable" among any other Python developers will get your point across.


As for "what is an iterate object?", you've probably mistaken the term with iterable or iterator which is what the for loop requires in order for it to work, a good break-down of those terms is already present in another StackOverflow question.

Community
  • 1
  • 1
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
2

I don't know if there's an "official" term for it, but I've seen it called iteration variable or loop variable. This is not specific to Python, it's common in many different languages.

Google Ngrams finds more hits for loop variable.

enter image description here

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I am not sure if this suits your definition of a "technical term", but the expression "loop variable" is used in quite a few PEP documents, e.g. for iterators and generators, though there seems to be no PEP on the basic for loop itself.

tobias_k
  • 81,265
  • 12
  • 120
  • 179