r/learnpython • • 29d ago

how do i overwrite a variable?

name = "Alice"

def func():
    name = "Bob"

func()

print(f"Hello, {name}!") # i want it to print bob
0 Upvotes

35 comments sorted by

View all comments

Show parent comment

4

u/JackandFred 29d ago

Yup evil demons no less.

It’s very rare for a variable to only show up once and never again. If it did you wouldn’t need to store the value in a variable. So having it changed globally means it will be changing the variable in all places at once, regardless of how it’s being used in different places at different times. It’s a common new programmer pattern that very often causes errors. Much better to have a well defined scope of how when and where you want a variable used.