-7
x=100
def fun2():
    print x
    x=10000
    print x
fun2()

The above program showing local variable x reference before assignment. Why it is not printing 100 10000

samba
  • 869
  • 4
  • 12
  • 20
  • 1
    Because `x` is here a *local variable*. And it errors on the first `print` since at that point, `x` is *not* initialized. – Willem Van Onsem Sep 13 '17 at 08:44
  • x is initialized in main block. why can't it referring from there. Run the above program without initializing X in the function. It will take 100 from the main block and print in the function. – samba Sep 13 '17 at 08:46
  • @samba: because regardless where you write `x = ...` in the scope, it sees `x` in that scope as local. It would be very confusing to first address a global variable `x`, and later a local variable `x`. – Willem Van Onsem Sep 13 '17 at 08:47
  • Could you please explain in the memory organization level... – samba Sep 13 '17 at 08:48
  • My question is why it's not able to take x value from main scope for first print statement in function – samba Sep 13 '17 at 08:59
  • this code is working as i expected in GOLang – samba Jul 29 '19 at 09:56

5 Answers5

0

x in the function is a local variable and can't access the other local variable you define first because they are in different scope.

Add global x to the start of your function or define x inside the function.

Quartal
  • 410
  • 3
  • 14
  • My question is why it's not able to take x value from main scope for first print statement in function – samba Sep 13 '17 at 08:59
  • since python first evaluates the whole function and already sees the assignment. And since global x is missing in the function (should be at the start of the function, before x is used) it assumes that you want to print an uninitialized local variable i.s.o. the global variable. And it sees that as an error. – Nemelis Sep 13 '17 at 09:14
  • I've just quickly tried your code-snippet. And I get an error which exactly tells you this: UnboundLocalError: local variable 'x' referenced before assignment. – Nemelis Sep 13 '17 at 09:18
  • Hi Quartal, There is compilation and execution steps in python. What do you mean by evaluation? – samba Sep 13 '17 at 09:23
0

You appear to not know about variable scoping.

The variable x does not exist in the function scope. You have to place global x before your print statement in order to access the global variable x.

x = 1 # Global x
def f():
    x = 2 # function-local x
    print(x) # prints 2
f()
print(x) # prints 1 because it uses the global x which remains unchanged
Martin B.
  • 1,567
  • 14
  • 26
  • but it exist in main scope. if you remove x initialization in the function, then it will take the x from main scope. Why it is not printing the main scope variable value in the local scope if i initialize it. Asper my view global x concept is different. I want to print main scope variable in to local scope and do some modification, which should not effect in main block again. If i go with global, then the modification will effect in global scope – samba Sep 13 '17 at 08:51
  • Think of the global x as G_x and of the local x as f_x. Unless you specifically say that you want the G_x when you type "x" python will assume you mean the f_x. These are 2 seperate variable. Go learn about variable scoping – Martin B. Sep 13 '17 at 08:52
  • My question is why it's not able to take x value from main scope for first print statement in function – samba Sep 13 '17 at 08:56
  • because x means something different in a function scope. if you place `global x` on the line before, then python knows that you are trying to access the x from the global scope. – Martin B. Sep 13 '17 at 08:57
  • can't we get the main scope variable in local scope without globle. execute the above program with out x=10000. It will print 100 and 100 – samba Sep 13 '17 at 09:01
0

If you want that to work you need to specify inside the function that the x variable you are using is the one in the global scope by using the global keyword.

x=100
def fun2():

    # Add this line
    global x

    print x
    x=10000
    print x
fun2()
Strinnityk
  • 548
  • 1
  • 5
  • 17
  • My question is why it's not able to take x value from main scope for first print statement in function – samba Sep 13 '17 at 08:58
  • It is able to, you just need to use the ```global``` keyword to specify the variable it's trying to access does exist, but is in the global scope. It's Python's way of making you say "Yes, I am sure I want to use a global variable." because of the issues global scoping causes. – Strinnityk Sep 13 '17 at 09:02
0

Below code will print the value of x -> 100, as it is there in main scope @samba, but when you change the value of it doesn't work that way as it is not defined in the function.

x = 100
def fun2():
  print(x)

fun2()

This doesn't work as the same way:

x = 100
def fun2():
  print(x)
  x = 1000
  print(x)

fun2()

and through error:

UnboundLocalError: local variable 'x' referenced before assignment


x is a local variable and not initialised in function fun2(). You need to understand variable scoping here, Please check Global and Local variable scope

If you want to use it globally use global keyword in your function.

bhansa
  • 7,282
  • 3
  • 30
  • 55
  • The main scope variables can be used in local scope. But please observe the program carefully. There it's not able to print the value in the local scope, which is defined in the main scope. – samba Sep 13 '17 at 08:58
  • see my comment I left at Quartal's answer (in answer of your question there) – Nemelis Sep 13 '17 at 09:16
  • *"The main scope variables can be used in local scope"* can we define it @samba. – bhansa Sep 13 '17 at 09:16
  • @Nemelis what is your concern about that ?, x is not initialised in the function. – bhansa Sep 13 '17 at 09:17
  • @bhansa: I tried to explain to samba that exactly that is the problem: local variable x is not initialised, since it is not declared in the function that the global x should be used. – Nemelis Sep 13 '17 at 09:20
  • I cannot do anything if he doesn't understand ;) You are good at your point. That's how the world works. – bhansa Sep 13 '17 at 09:22
  • Run my program without initializing in the local scope. Remove x=10000 from the function, then it will print 100 and 100. Why it;s not able to take x 100 if i initialize in the function? – samba Sep 13 '17 at 09:27
  • Since the function is evaluated by python and python sees the assignment x=10000 before it executes the first print statement and thus assumes you want to print a local variable and not the global variable. By putting global x before the first print statement python knows that you want to use the global x. You don't need global x if you only want to print x, since then python will look at the global variables when it does not see x in the local variables. – Nemelis Sep 13 '17 at 09:29
  • So to use gobal variables need to use global keyword?. As per my view global keyword can be used to effect in global scope, but not to get the global scope variables. You can access global scope variables in to local without global keyword. In python statements can be execute's sequentially. If this is the case how initialization executes before print? – samba Sep 13 '17 at 09:34
  • You only need the global keyword when you are changing the value of the variable in the function, like you do. This so python knows that you want to change the value of the global variable. If you only use it and not change it, you don't need the keyword. – Nemelis Sep 13 '17 at 09:36
  • @samba I suggest you should go through the links, have your thoughts and discuss again. Please don't make the comments as a chat section. – bhansa Sep 13 '17 at 09:36
  • Thanks for your efforts Nemelis. The given links saying that, we could not use single variable as local and global, but i could not believe this. Could you please explain in the memory organization level. Because this is totally contreduction to my thoughts on python. – samba Sep 13 '17 at 09:52
-2

Because u assigned variable before function. Just try this

def fun2():
  x=100
  print x
  x=10000
  print x
fun2()

It will output 100 and 1000

  • I want to defined x in main block only.. – samba Sep 13 '17 at 09:04
  • there are some answer here which explains solution to problem , why you think it's different? it's even less detail. however you are about to use global variable – Gahan Sep 13 '17 at 09:04
  • Hi Andrew, My question here is why it's not able to take the value from main scope to local scope. This you can if you remove the initialization part in the function. Means remove x=10000 in the function and run my program. It will take x value from main scope. But if you write any initialization of that variable. we could not able to use that before initialization in local, even though that is initialized in the main scope – samba Sep 13 '17 at 09:20
  • so google it and don't minus my answers – Andrew Konstantinov Sep 13 '17 at 10:16