0

I'm quite new to Python and I learned something interesting.

item = 5

for item in [1, 2, 3]:
    pass

print(item)

Result:

3

I expected 5 because I thought the variable inside the a for loop would be local to its scope but it wasn't.

I think it is not good since I might accidentally write global variable name in a for loop.

How do people usually deal with the potential problem? Is there a way to make the variable local to the scope?

Zack Lee
  • 2,784
  • 6
  • 35
  • 77
  • 1
    Loops don't have their own scope. Python isn't all that odd in that regard. I have never found it to be a problem. It is only a problem if you try to bring scope ideas gleaned from e.g. Java to Python. – John Coleman Oct 27 '20 at 23:16
  • Give them different names and they will be different variables. – kaya3 Oct 27 '20 at 23:19
  • I have never had this problem in other languanges but python. It sucks python doesn't have local variable. – Zack Lee Oct 27 '20 at 23:21
  • 1
    To be fair, Python is a bit odd. Comprehensions, for example, have their own scope (in Python 3) but `for` loops don't. – Selcuk Oct 27 '20 at 23:21
  • What would be the downside of making the variable local in a for loop. I think it will only be safer. – Zack Lee Oct 27 '20 at 23:22
  • 1
    @ZackLee Other languages such as JavaScript and PHP support this behaviour too. Python is not the only one. This has been discussed before: https://stackoverflow.com/questions/3611760/scoping-in-python-for-loops – Selcuk Oct 27 '20 at 23:23
  • 1
    @ZackLee The downside of making the variable local in a for loop is that it would then be unnecessarily tedious to access its last value after the loop ends, particularly if the loops is ended with a `break` statement. – blhsing Oct 27 '20 at 23:28
  • @Selcuk This question is basically a duplicate of that other one. Good catch. – John Coleman Oct 27 '20 at 23:29
  • @selcuk In Javascript, i can use 'let' to prevent it from accessing the global variable. – Zack Lee Oct 27 '20 at 23:29
  • @ZackLee Python *does have local variables*, it just doesn't have *block scope*. – juanpa.arrivillaga Oct 27 '20 at 23:36
  • If you are worried about a for-loop cluttering your global namespace, your for-loop should probably not be in the global scope to begin with – juanpa.arrivillaga Oct 27 '20 at 23:38
  • @juanpa.arrivillaga Actually all Python variables are local to their code blocks unless declared with `global` or `nonlocal`. It's just that what's considered a code block is different in Python than in some other languages. A module, a function or a class is a code block. A for loop is not. – blhsing Oct 27 '20 at 23:39
  • @ZackLee Please read: https://docs.python.org/3/reference/executionmodel.html – blhsing Oct 27 '20 at 23:42
  • @blhsing I'm using the word "block" in the sense of the [grammer](https://docs.python.org/3/reference/grammar.html). The loop body is called a "block". Basically, a block is a continuous section of code indented at the same level. What might often be delimited by curly braces in C-like langauges – juanpa.arrivillaga Oct 27 '20 at 23:43
  • 1
    @ZackLee True, but `let` is a recent addition. Similarly comprehensions have their own scope in Python 3. You claimed that no other languages but Python shows this behaviour, but that's not correct (we can also add Ruby to the list). – Selcuk Oct 27 '20 at 23:54
  • @Selcuk I would rather first have constants in Python than block scope. I've never once missed block scope, but I do often wish for constants. Although it isn't clear the best way to allow that to happen. Javascript had variable declarations, which python doesn't have; although, you could probably add a `constant` keyword just like `global`. In any case, originally JS scoping rules were pretty rough in that assignment to variables in a local scope would *default to global* leading to a lot of bugs. Python, famously, has the opposite defaulting to local. – juanpa.arrivillaga Oct 28 '20 at 00:02
  • 1
    @juanpa.arrivillaga I'm personally quite happy with the PEP-8 conventions for constants (uppercase names). Scopes are also fine (leaking comprehension variables was a bit weird and fortunately they have fixed that). I am still trying to get over the walrus operator. – Selcuk Oct 28 '20 at 01:09

0 Answers0