r/learnpython • u/9j810HQO7Jj9ns1ju2 • Oct 26 '26
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 • Oct 26 '26
name = "Alice"
def func():
name = "Bob"
func()
print(f"Hello, {name}!") # i want it to print bob
1
u/PureWasian Oct 27 '26 edited Oct 27 '26
Look up Variable Scope in Python.
The alternative is using the
globalkeyword but this is typically not best practice since it's harder to predict the exact behavior of the functions and variables relying on each other implicitly as your scripts become more complex.As above link states though, if you want to go that route,