Python variable confusion

I came across some kind of code that puzzled me. Here is a minimal example that shows this:

# of course, the ... are not part of the actual code some_var = {"key1":"value1" ... "keyN":"valueN"} def some_func(): v = some_var["key1"] 

The code works, but the fact that I can access some_var directly confuses me. The last time I had to write Python code, I remember that I had to write some_func as follows:

 def some_func(): global some_var v = some_var["key1"] 

I am using Python 2.7.1 on a Windows 7 PC. Has something changed in version 2.7 that allows this?

+7
source share
6 answers

No, you simply cannot reassign some_var in the local scope. Consider the following:

 some_var = {} def some_func(): # some_var[5] = 6 some_var = {1:2} some_var[3] = 4 some_func() print (repr(some_var)) # {} 

You will see that the assignment in some_func actually creates a local variable that obscures the global one. Thus, uncommenting the string will result in an UnboundLocalError - you cannot access the variables until they are defined.

+6
source

You need to use global if you want to assign a new value to this variable.

A nested scope was introduced in Python 2.1 (and is enabled by default in Python 2.2 ) (highlighted by me):

Simply put, when a variable name is not assigned a value inside a function (by assigning either the def , class or import statements), the variable references will be checked in the local namespace of the closing area . A more detailed explanation of the rules and disclosure of the implementation can be found in PEP .

+3
source

There is a difference between using (for example, calling or using in an expression) a name from the outside and assigning it (and there is a difference between assigning an empty variable and assigning a member of the object that the variable points to - xy = ... and x[...] = ... count method calls!).

You only need to declare that the variable is from the outer scope if you reassigned it. In Python 2, you can only do this with global variables (via global var ), in Python 3 you can do this for absorbed nested areas (e.g. closures) using nonlocal var .

Using a non-local variable, as in your example, does not require global . However, as soon as you assign a variable (with the above definition of purpose) anywhere in this area, add the string some_var = ... after the line in which you use it and you get an UnboundLocalError . See the documentation for more details.

+3
source

You need to use global if you intend to assign a variable, it is not necessary to read the variable . This difference is not arbitrary, although it may seem at first glance.

When reading a value, the interpreter can simply look for a local variable called some_var , if it cannot find it, then it looks for a global variable of that name. This is a simple and understandable semantics.

When assigning values ​​to a variable, the interpreter must know whether you intend to assign a local variable some_var or a global variable. The interpreter assumes that some_var = 2 when calling inside a function assigns a local variable, this makes sense, since this is the most common case . In relatively rare times, when you want to assign a global variable from within a function, you use the global modifier global some_var = 2 .

+2
source

It depends on the use of the variable in the Python variable region function Error

+1
source

Assigning a value to a name makes the name local if the name is not explicitly declared global.

 a = 12 def foo(): a = 42 print a # uses local foo() >>> 42 def foo(): global a a = 42 foo() print a >>> 42 

If no name is assigned, it is global.

 a = 12 def foo(): print a # uses global foo() >>> 12 

In short, you need to explicitly declare a global name if you assign it. If you just read it, you can use it as you wish. However, if you ever assign this variable, it will be considered local in this function if you have not declared it global.

 b = 5 def foo(): print b b = 7 foo() >>> ??? 

Since b is assigned in foo() and is not declared global, Python decides at compile time that b is a local name. Therefore, b is the local name in the entire function, including in the print statement before assignment.

Therefore, the print statement gives you an error, since the local name b not defined!

+1
source

All Articles