0

I'm starting the basics with , and I didn't get how to update a global variable. The code is:

def spam():
    global eggs
    eggs='spam'
def update_eggs():
    global eggs;eggs='global'
print(eggs)

I wanted to overwrite the "eggs" variable set as "spam" by "global", and I tried the methods in this post: Overwrite global var in one line in Python? But it didn't work. Could you advise please? Thank you in advance! Silvia

Silvia Wu
  • 71
  • 4
  • 1
    I strongly discourage the pursuit of "do this in one line" where python is involved. Python is expressive and whitespace sensitive. I encourage you to embrace that. – JonSG Mar 29 '22 at 19:17
  • Python has no `;` at the end of line/statement – eri0o Mar 29 '22 at 19:18
  • Just a note that your code currently does what you seek to do, you just have to actually call one of those functions. – JonSG Mar 29 '22 at 19:19
  • @eri0o it *requires* no semi-colon, but semi-colons can be used to seperate simple statements on a single line, e.g. `x = 'foo'; print(x)` so a semicolong at the end of a line is probably valid, although highly unidiomatic – juanpa.arrivillaga Mar 29 '22 at 19:32
  • 1
    @juanpa.arrivillaga While I follow what you said and agree, I might update that to "*a semicolon is not required*" rather than "*it requires no semicolon*" which implies to me that it would be an error in include one. – JonSG Mar 29 '22 at 19:36

2 Answers2

4

You need to call the function for it to execute the code inside it.

eggs = 'something'
def spam():
    global eggs
    eggs='spam'
def update_eggs():
    global eggs;eggs='global'

spam() <-- Call function
print(eggs)

This will (marked with an arrow) call the function spam so it updates eggs to spam. Hope this helps.

Additionally, it looks like you did not define the global variable "eggs". You need to mention eggs = "something here" at the beginning of your code. The "global" command makes it so that defined functions are able to edit the global variable eggs after you define it.

Alan Shiah
  • 907
  • 1
  • 6
  • 19
  • 3
    Technically defining `eggs` in the global scope is not required here as long as one of those functions is evoked prior to accessing it. Try commenting out your `eggs = 'something'` and run it. Though I would agree that it is best practice to do so. – JonSG Mar 29 '22 at 19:22
  • ^ This also works. If you call your function that defined `eggs` as a global variable before trying to print, you do not need to define it at the beginning of the script as it was already made a global variable by your function. – Alan Shiah Mar 29 '22 at 19:25
  • Many thanks for your reply! Actually I need to call the updated function. Others worked perfectly! Thank you. – Silvia Wu Mar 30 '22 at 19:05
2

The global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.

It is good practice to intialize the global variable in the main context before it is referenced.

eggs = "global" # <-- global variable initialized

def spam():
    global eggs
    eggs = 'spam'

def update_eggs():
    global eggs
    eggs = 'update'

print(eggs)
spam()        # <-- updates global eggs variable
print(eggs)
update_eggs() # <-- updates global eggs variable
print(eggs)

Output:

global
spam
update

Calling spam() or update_eggs() both update the global variable eggs.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75