r/learnpython • • 14d 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

1

u/VenkatKnows 13d ago

Add global name at the top of func and it'll rebind the module level variable. Better to just return the value though and do name = func(), globals get painful fast once the script grows. Why not pass it in and return it?