I'm a bit confused with the variable scope of Python. Perhaps because of being used to the convention of C++, I always made some mistakes in variable scope of Python. For example:
in C++:
int main(){
int i = 3;
for (int j = 0; j <= 3; ++j){
i += 1;
}
cout << "i = " << i << endl;
cout << "j = " << j << endl; //Error, out of 'for' scoping.
return 0;
}
But in Python:
i = 3
for j in range(1,4):
i += 1
print j # j = 3, nothing wrong
for i in range(5,7):
j += 1
print i # i = 6, changed by operation in for loop
This is just a simple example, and I'm not going to list other differences. Could anyone please give a detailed explanation of their differences in scoping.
Python doesn't provide a block scope. The minimum scope for a variable is the function (like it happens in pre-ES6 Javascript).
The original reason for this design (if I understood correctly) is that if you need block-scope variable then the code is probably too complex anyway and factorizing out a function is a good idea (note that you can create local functions/closures in Python so this doesn't necessarily mean the code will need to be spread and delocalized like it would happen with C).
As an exception to this "no-block-scope" rule, with Python3 variables used inside comprehensions were made local to the comprehension, i.e. after
x = [i*i for i in range(10)]
i
will be 9 in Python2, but no variable i
will leak from the expression in Python3.
Python rules for scoping are not very complex: if there are assignments (or augmented assigments like +=
-=
and so on) in the body of the function then the variable is considered a local, if instead it's only accessed for reading the variable is considered coming from an outer scope (or a global if the function is at top level).
If you need to modify a global in a function (not something that should happen often) you need to declare it explicitly with global
.
In Python3 it's also possible to access a local variable captured in an inner function for writing by using a nonlocal
declaration. In Python2 instead captured local variables can only be accessed for reading in inner functions (assignment would make them locals of the inner function and global
declaration would make them globals instead).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With