Python Forum

Full Version: global var in def
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have text form e and button wich save value of this form to global var

button_add = Button(root, text="+", command=button_add)
Can you explain, why this code work

def button_add():
    first_number = e.get()
    global f_num
    f_num = int(first_number)
But this not

def button_add():
    global f_num = int(e.get())
Error:

Error:
global f_num = int(e.get()) ^ SyntaxError: invalid syntax
For me they are same, but not(
Thanks!
Look at the definition: 7.12. The global statement. The definition allows to declare a variable as global, but not to assign a value to it. So you must do that in two lines.