2

In PHP or Python (other languages may apply), why does a value declared in a for (or foreach) still exists (and overwrites) in the outer scope?

For example, in PHP:

<?php

$value = 0;
$test = [1,2,3];

foreach ($test as $value) {
    echo $value;
}
echo $value;

(execute it (also with a for loop) here)

will output:

1233

and in Python:

value = 0
test = [1,2,3]

for value in test:
  print(value)

print(value)

(execute it here)

will output:

1
2
3
3

It may apply in other languages I'm not still aware of.

The question is why are these for blocks designed like this? Why it isn't like (for example) C, where the indexing variable only exists in the for scope (example here)?

6
  • 2
    It's in the philosophy of the language; each language with its own. C != Python Commented Jan 19, 2017 at 8:46
  • It's a fair point. I learned programming the old way, with C, so I'm biaised. But then, why has it been designed like that? Commented Jan 19, 2017 at 8:47
  • I'm not GvR . . . Commented Jan 19, 2017 at 8:49
  • Honestly, I don't know if someone can answer your question here. As moses said, its the philosophy of the language. Its simply another idea. Like there are frameworks with other philosophy, there are languages. I think for a better answer you have to contact the PHP group themself. May they can say you what their thoughts were while developing. Commented Jan 19, 2017 at 8:53
  • 1
    Thanks @godaygo, the mail linked in the answer has a very interesting line: "I think this arrangement would allow almost all existing code to continue working, and new code to be written that takes advantage of final values of loop variables." So the reason in Python is likely to be "it has been so at one point and since scripts may be using it we don't want to break them by changing the behavior" Commented Jan 19, 2017 at 9:01

1 Answer 1

1

Check out this answer to a more generic question about python scopes.

You can see that for-loops don't influence the scope at all. There is no new scope being created. Inside the loop you create, read and update variables the same way as outside.

Your question can be rephrased to "why doesn't python use block scope?". Wikipedia has a lot of info about this topic: en.wikipedia.org/wiki/Scope_(computer_science)#Levels_of_sco‌​pe

From other answers here on SO there's the theory that block scope would make things too complicated and you're supposed to keep functions short anyway. If you need a new scope, create another function and call it.

Sign up to request clarification or add additional context in comments.

3 Comments

So I may have a wrong wording on "for scopes". With this in mind, the question would become "why isn't a new scope created?". I'm emphasing the why, because I'm (now even more) aware of the "no new scope is created".
Do you care to put your comment in an answer so I can accept it? It pretty much sums up everything.
I'm glad you like it, i moved the comment to my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.