1

when coding in python 2.7, I have a problem about the interal environment of function. Here is the simple sample code:

m=3

def f(x):
    return m*x

when I type f(5) in Python Console, it returns 15, which is in line with my expectation. However, when I edit the code to:

m=3

def f(x): 
    m=m+1
    return m*x

the console returns UnboundLocalError: local variable 'm' referenced before assignment, and I do not understand why.

I think there is no difference for variable m between these two cases. However, one is correct while the other isn't.

Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
3.1415926
  • 19
  • 3
  • 3
    You can read but not modify a global without explicitly declaring it `global`. Global variables are an antipattern, though; you really, really want to avoid them, especially while learning. – tripleee Jun 27 '16 at 06:27
  • Also, since you are trying to modify it without declaring it global, it is interpreted as a local variable which is why you are getting this error. – gaganso Jun 27 '16 at 06:29

3 Answers3

1

This happens because in the first case there is no ambiguity about m i.e. it is a global. In the second case, however, the usage of m in m=m+1 throws as error because python looks for a local named m and it does not find any. You will have to explicitly declare that you're using a global. The following will work.

m=3

def f(x): 
    global m
    m=m+1
    return m*x
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
0

Thanks! However, please refer to the code below:

m=3 def f(x): global m m=m+1 print(m)

def g(x): print(m)

when I type f(3) in the console, it returns 4; then I type f(3) again, it returns 5; then I type in g(3), it returns 5. AND then, when I type in m, it returns 3. Why? Thanks!

3.1415926
  • 19
  • 3
0

Global variable

In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state.

 m = 0

 def f(x):
     print(m)
     return x

m variable scope can be accessed out of the function f(x).

Local variable

A local variable is a variable which is either a variable declared within the function or is an argument passed to a function. As you may have encountered in your programming, if we declare variables in a function then we can only use them within that function.

 def f(x):
     m = 0
     print(m)
     return x

m can only be accessed in function f(x).


~ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> m = 3
>>>
>>>
>>> def f(x):
...     global m
...     m = m + 1
...     print(m)
...
>>>
>>> def g(x):
...     print(m)
...
>>>

Please see the execution flow:

>>> m       <---- original value
3
>>> f(3)    <---- m = 3; m = m + 1; m = 4
4
>>> m
4
>>> f(3)    <---- m = 4; m = m + 1; m = 5
5
>>> m
5

Wish this can help you.

debug
  • 991
  • 10
  • 14