python - why variable doesn't store stuff when inside a def funct -


i'm new programming! when do:

>>>x = "heyy" >>>def hello():        x = "hi" >>>hello() >>>print(x) heyy 

why x still "heyy"? there anyway can turn x = hi global variable? without using "return" , putting new variable? like:

>>>x = "heyy" >>>def hello():     x = "hi"     return x >>>y = hello() >>>print(y) hi 

basically want change contents of x hi instead of heyy when run hello()

you can use global keyword so

def hello():    global x    x = "hi" 

but note bad practice , return should used ever possible. because there every chance of value of variable getting corrupted in of methods


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -