0

I got a message 'local variable referenced before assignment'

Use external variable and got OK, but failed to assign value

x = 10
y = 10

def some():

    print(x)

some()

10
x = 10
y = 10
def some():
    x = 100-x+x
    print(x)
some()

local variable 'x' referenced before assignment
x = 10
y = 10
def some():
    t=100-x
    print(t)
some()

90
x = 10
y = 10
def some():
    t=100-x
    x=t
    print(t)
some()

local variable 'x' referenced before assignment

What's the differences ? Expected result should be the same, but failed in 2nd sample.

Does it mean what I can do just read from 'x', no write to 'x' ?

Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • 1
    Possible duplicate of [referenced before assignment error in python](https://stackoverflow.com/questions/855493/referenced-before-assignment-error-in-python) – Andrej Kesely Aug 17 '19 at 05:43
  • Not about how to use global declaration, just to confirm why how you use the variable with same name as outside of function, and get the different variable declaration by intepreter. – Jason Yang Aug 17 '19 at 05:53
  • I think a variable should be defined as a local variable , or global variable not depends on how it used in the subroutines. – Jason Yang Aug 17 '19 at 14:07

1 Answers1

1

When you declare a variable inside a function it shadows the variable from the outer scope that has the same name. Once you declared x inside the function it will become the x variable for all the commands in the function, even before the outer scope assignment where x = 10.

If you want to write to the x variable in the outer scope you should declare it as global x, that is

x = 10
y = 10
def some():
    global x
    x = 100-x+x
    print(x)
some()

100
d e
  • 13
  • 2
Rotem Tal
  • 739
  • 5
  • 11
  • I know it can solve my problen, but if thousnd items here, so what ? I want to know why it is, but not to get the result only. – Jason Yang Aug 17 '19 at 13:25
  • The difference is, that once you define the variable `x` inside your function, it becomes the only `x` the function would be aware of,or, the function's variable SHADOWS the global variable, that is the case for the entire function, not just the lines after the assignment, which is why the 2nd example behaves differently than the 1st – Rotem Tal Aug 17 '19 at 13:32
  • If, and if, without global declaration, how can we use the same variable name in sub-routine to do some iterations and, finally, variables keeps the result. There maybe lot of varibales, thousand items ? global declarations were not good for it. – Jason Yang Aug 17 '19 at 14:01
  • using such global variables is usually considered a bad practice, shadowing variables is considered bad as well since it reduces the readability, so the best solution would be to avoid both, if you must assign to a global variable you have no choice but declare it global, if you only wish to use its value, then the best practice would be to not shadow his name – Rotem Tal Aug 17 '19 at 14:26
  • "once" you define the variable x inside your function .... Is that mean I cannot define the variable, just using it ? x= x+1 will change the definition of variable 'x' from global into local ? If without global declaration and without return values, does it mean I cannot get the new values after function/method finished. – Jason Yang Aug 17 '19 at 14:27
  • without declaring `global x` you cannot use `x=x+1` (unless `x` was already defined in your function, in which case the outer scope `x` would not be affected). A better practice would be to send `x` as argument, and if you wish to change his value do so by either returning or declaring it global – Rotem Tal Aug 17 '19 at 14:30
  • For other language, always with variable declaration statment. But for Python, the answer is no, variable will be automatically defined by intepreter. I got lot of failures or bugs in my script and waste lot of times to find it. Anyway, thank you for your explanation !! – Jason Yang Aug 18 '19 at 03:19