2

I wrote this code in Python as I was trying to understand the global statement -

eggs=24

def f():
 print(eggs)
 eggs=25
 print(eggs)

f()
print(eggs)

The output should be

24
25
24

Python should see eggs = 24 as a global variable and when the function f() is called, it should print eggs=24 as the local value is not assigned to eggs till now. Then a local value of 25 should be assigned to eggs and 25 should be printed on the screen after 24. After the function returns, eggs should be assigned it’s global value that is 24 and 24 should be printed on the screen at last.

But I got an error message saying that “UnboundLocalError: local variable 'eggs' referenced before assignment”.

Where am I wrong in understanding of how Python runs this function?

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Aaryan Dewan
  • 204
  • 3
  • 11
  • 5
    When there's a local assignment, global lookup is skipped. I'm sure there's a dupe somewhere. – Łukasz Rogalski May 24 '17 at 14:24
  • 2
    Possible duplicate of [Using global variables in a function other than the one that created them](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – Raja Sattiraju May 24 '17 at 14:30

2 Answers2

3

Duplicate of (Local variable referenced before assignment in Python?)

This should do the trick

eggs = 24

def f():
    global eggs

    print(eggs)
    eggs = 25
    print(eggs)

f()
print(eggs)

There are tons of questions like these on Stack Overflow and when you are new to Python it is hard to understand it's behaviour in this particular situation, since in practically any imperative language a code like this will run totally ok

var x = 1;

//prints 1 to console
console.log(x);

function f() {
    x = 2;
}

f();

//prints 2 to console
console.log(x);

But for Python your code looks something like this

var x = 1;

console.log(x);

function f() {
    I_AM_NOT_X_EVEN_THOUGH_I_HAVE_A_NAME_X = 2;
}

f();

console.log(x);
papadoble151
  • 646
  • 8
  • 19
  • That worked! But my question was why doesn’t my code work? Could you please simply one of the answers to the question in the link you mentioned because I am a newbie. – Aaryan Dewan May 24 '17 at 15:09
  • 1
    @Aaryan Dewan When you refer to the eggs variable for the first time in the f function (print(eggs) command) python assumes that eggs is a local variable and since it is local, it has no value assigned. Or in other words, the eggs in the function you defined are not the eggs from the first line. See a similar mistake explained here [link](https://stackoverflow.com/a/18002911/6294253) – papadoble151 May 24 '17 at 15:15
  • “When you refer to the eggs variable for the first time in the f function (print(eggs) command) python assumes that eggs is a local variable and since it is local, it has no value assigned”. Suppose I defined the function like this - def f(x): print(eggs). It would have simply printed 24. So I don’t think what you said holds every time! – Aaryan Dewan May 24 '17 at 15:30
  • @Aaryan Dewan, when you define a variable inside a function it is local to this function by default. It means that you have created new eggs, and the eggs you have created above and assigned a value to, simply do not exist. It doesn't hold for C++ or Java, definitely, but it does hold for Python – papadoble151 May 24 '17 at 15:37
1

You can use a global variable in other functions by declaring it as global in each function that assigns to it:

eggs=24

def f():
 global eggs
 print(eggs)
 eggs=25
 print(eggs)

def onlyRead():
 print(eggs)

f()
onlyRead()

See reference

papadoble151
  • 646
  • 8
  • 19
developer_hatch
  • 15,898
  • 3
  • 42
  • 75