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

r - Trouble relying on third party package imports in my package -

Payment information shows nothing in one page checkout page magento -