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)?

JayIsTooCommon
  • 1,714
  • 2
  • 20
  • 30
Trucy
  • 146
  • 2
  • 14
  • 2
    It's in the philosophy of the language; each language with its own. C != Python – Moses Koledoye Jan 19 '17 at 08: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? – Trucy Jan 19 '17 at 08:47
  • I'm not GvR . . . – Moses Koledoye Jan 19 '17 at 08: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. – Twinfriends Jan 19 '17 at 08:53
  • You're right. I'll definitely contact them to get a proper answer. – Trucy Jan 19 '17 at 08:57
  • 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" – Trucy Jan 19 '17 at 09:01

1 Answers1

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.

Community
  • 1
  • 1
fafl
  • 7,222
  • 3
  • 27
  • 50
  • 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". – Trucy Jan 19 '17 at 08:42
  • Do you care to put your comment in an answer so I can accept it? It pretty much sums up everything. – Trucy Jan 20 '17 at 09:17
  • I'm glad you like it, i moved the comment to my answer – fafl Jan 20 '17 at 09:29