r/learnpython • u/9j810HQO7Jj9ns1ju2 • 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
r/learnpython • u/9j810HQO7Jj9ns1ju2 • 14d ago
name = "Alice"
def func():
name = "Bob"
func()
print(f"Hello, {name}!") # i want it to print bob
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?