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
View all comments
Show parent comment
1
u/LayotFctor 29d ago edited 29d ago
Bugs like they said. Global variables are variables that are available to the entirety of your code. Every function can edit it, no function really knows what has or has not been done to it, and in what order. You will very easily get untraceable errors that are unnecessarily difficult to track down. It looks easy and convenient, but is actually a trap as it's the hardest kind of variable to use correctly.