Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences (if exist) in variable scope between Python and C++? [duplicate]

Tags:

c++

python

scope

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.

like image 890
user123 Avatar asked Sep 10 '25 16:09

user123


1 Answers

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

like image 191
6502 Avatar answered Sep 13 '25 06:09

6502