0

I understand that the python in operator is overloaded but I don't understand how the interpreter distinguishes different uses; Nor can I find Documentation to understand it.

Why is

x = 1 
for x in (1,2,3):
    print(x)

not parsed as


for true


Colin Hicks
  • 348
  • 1
  • 3
  • 13
  • 3
    [See also](https://stackoverflow.com/questions/38204342/python-in-keyword-in-expression-vs-in-for-loop) – Mark Jun 28 '19 at 23:54
  • 4
    `in` is not an operator in this context. It's part of the `for` statement. It separates the `for` variable(s) from the iterable. – Tom Karzes Jun 28 '19 at 23:56
  • 1
    @MarkMeyer thank you for that, that for the most part answered my question. Im guessing that once python encounters the for keyword, when it runs into *in* it no longer treats it like it would in other expressions? why is in used in for loops then? I feel like a different keyword would cause less confusion – Colin Hicks Jun 29 '19 at 00:07
  • 1
    This is *not* an `x in y` expression. The `for` results in a different production rule in the Python grammar. – Willem Van Onsem Jun 29 '19 at 00:11

0 Answers0