-2

So made a simple example to help better understand scopes for myself.

From what I understand, a variable within a function is local to ONLY that function. If you want to use a variable that's outside of your function that has the same name as a local variable within your function, you can use the global keyword which tells your function to ignore any local variables with that name and only use the global variable.

So this program is supposed to print the Global variables value first, then call the function and print the global variable again since it should ignore the local variable, but it prints the local variable anyway. Python tutor shows me that it completely skips the global statement.

 cheese = 99
    
    
    def func():
        global cheese
        cheese = 100
        print(f"Global cheese is {cheese}")
    
    
    
    print(f"Global cheese is {cheese}")
    func()

Output:

Global cheese is 99

Global cheese is 100

Then when I call the function before the print statement, the variable gets changed to the local variable's value.. why is this?

cheese = 99


def func():
    global cheese
    cheese = 100
    print(f"Global cheese is {cheese}")



func()
print(f"Global cheese is {cheese}")

Output:

Global cheese is 100

Global cheese is 100

  • 6
    What local variable? You don't have any. – Manuel Mar 24 '21 at 01:56
  • 2
    Either you're misreading the Python tutor thing, or the Python tutor thing is just wrong. There's no local variable here. – user2357112 Mar 24 '21 at 01:57
  • cheese = 100. Is that not my local variable? – Johnny Silverhand Mar 24 '21 at 01:57
  • 3
    No, you're assigning to a global, because of the `global cheese` in the same function. – user2357112 Mar 24 '21 at 01:58
  • 6
    Your understanding of what `global` does - "If you want to use a variable that's outside of your function that has the same name as a local variable within your function, you can use the global keyword which tells your function to ignore any local variables with that name and only use the global variable." - is wrong. `global` doesn't make Python ignore local variables. If you use `global cheese` in a function, that function does not have a `cheese` local variable at all. There is no local variable to ignore. – user2357112 Mar 24 '21 at 02:00
  • I want to make sure I understand what you're asking. You try to print the global value *before* using the function that changes the global value, and it shows you the unchanged value, and you don't understand why this happens? You expect it to show the changed value? But it hasn't been changed yet! Why would you expect this? – Karl Knechtel Mar 24 '21 at 02:05
  • 1
    "then call the function and print the global variable again since it should ignore the local variable, but it prints the local variable anyway". No, it prints the global variable, because there *isn't* a local variable. It seems like you're expecting it to still show you `99`, even though you just wrote `cheese = 100`, because you're expecting that to be a separate `cheese` from the global one. The *entire point* of the `global` statement is to make it *not* be a separate `cheese`. – Karl Knechtel Mar 24 '21 at 02:08
  • *Without* the statement, you get a separate local variable (because you did an assignment). "But either way, the `cheese` in `cheese = 100` has to be the same `cheese` as in `print(f"Global cheese is {cheese}")` (inside the function). – Karl Knechtel Mar 24 '21 at 02:09
  • "Python tutor shows me that it completely skips the global statement." Yes, because that statement *doesn't correspond to an actual action*; instead, it tells Python how to interpret *other* lines of code. – Karl Knechtel Mar 24 '21 at 02:09
  • Alright, glad I asked. Gotta learn somehow right. – Johnny Silverhand Mar 24 '21 at 02:10
  • The way to learn is to re-read your reference material, or use a proper forum website such as https://reddit.com/r/learnpython (after reading their rules and FAQ). Stack Overflow is not intended to replace existing tutorials and documentation. – Karl Knechtel Mar 24 '21 at 02:11
  • Alright Karl... – Johnny Silverhand Mar 24 '21 at 02:12
  • So if I understand now, global changes our local variable into the global variable. So the local variable gets destroyed and instead we only have one variable left and that's our global variable. When I change cheese within my function, I'm really changing global cheese instead. I was under the impression that "both" variables got changed when in reality, I only had one and that was the global one. Thank you Karl. – Johnny Silverhand Mar 24 '21 at 02:38

3 Answers3

1

But you declare that you want to use the global variable in the function. So when you assign a value it changes the global variable.

cheese = 99
    
    
def func():
  global cheese # Declares that you want to use global cheese variable
  cheese = 100  # This updates the global cheese variable
  print(f"func() cheese is {cheese}")
    
def local_func():
  cheese = 101
  print(f"local_func() cheese is {cheese}")


print(f"Global cheese is {cheese}")        
func()
print(f"Global cheese is {cheese}")
local_func()
print(f"Global cheese is {cheese}")

The results are:

Global cheese is 99
func() cheese is 100
Global cheese is 100
local_func() cheese is 101
Global cheese is 100
teambob
  • 1,994
  • 14
  • 19
0

the global keyword is used to create a global variable inside a function

In your case when you try write global cheese it re-creates a global variable called cheese which you assign the value 100. thus it ignores the previously created cheese variable with value 99

here is a better explanation for the global keyword

Me Bottle O Scrumpy
  • 266
  • 1
  • 3
  • 12
0

When your function runs, it uses the global cheese variable instead of the local cheese variable as you have mentioned.

So when your function runs cheese = 100 statement, it's referring to the global cheese variable that you have declared in the beginning of the program. The statement does not create a new local cheese variable.

As the result, running func() will update the global variable to 100.

hyunchel
  • 161
  • 2
  • 13