The reason behind Python's scope sharing rules is a consequence of Python's choice not to have explicit declarations of local variables. Without such declarations, C++-style scoping rules where every nesting creates a new scope would make the following fail:
def test():
if some_condition:
a = 3
else:
a = 5
# If the above nesting created a new scope, "a" would
# now be out of scope.
print a
That works in C++ because explicit declarations provide you with choice between writing:
// doesn't work
if (some_condition) {
int a = 3;
}
else {
int a = 5;
}
// "a" is out of scope
std::cout << a ;
and:
// works
int a;
if (some_condition) {
a = 3;
}
else {
a = 5;
}
printf("%d\n", a);
Python, having no explicit declarations for local variables, must extend the scope to the whole function definition (and class definition in case of class scope).