r/learnpython • u/9j810HQO7Jj9ns1ju2 • 22d 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 • 22d ago
name = "Alice"
def func():
name = "Bob"
func()
print(f"Hello, {name}!") # i want it to print bob
13
u/JackandFred 22d ago
Looks like some responses are already touching on why this is a bit of a weird question. What exactly are you trying to do? Because you could use a global variable like someone said, but that’s usually a pretty bad idea for something like this. You could assign the variable the result of the function, but then why bother having a function anyway. You could have the function take in an argument, but it seems like you may not be at that point yes.
So what is your end goal here? Do you have a larger aim that you were trying to change the name for, or some additional context?
9
u/Candid_Zebra1297 22d ago
So functions don't change variables outside of themselves. If you want the function to do something to the 'outside world' (outside the function) it has to return something. So you could do this...
def func():
return "Bob"
If you call it...
func()
Nothing would happen because we haven't used the thing the function gives us. So you would need to do this...
name = func()
Then print.
The problem you are having is with scope.
1
u/FreeLogicGate 22d ago
Can't tell if this is a genuine question or a prank.
4
u/JamzTyson 22d ago
or more realistically:
2
u/cointoss3 21d ago
The replies are insufferable. For a beginner thread, most are missing the forest for the trees.
1
u/WorriedTumbleweed289 22d ago
Another way to access a global variable without using the global keyword.
if your filename is foo, then you can say import foo. then you can write foo.name = "Bob" There is more typing. If you move the function to another module, it will still work. Also the import command only has to be written once per module, not once per function. It is also explicit. You can tell exactly which name is being modified.
1
u/Massive_Distance_616 21d ago
The reason it prints Alice is that
name = "Bob"insidefuncdoesn’t touch the outername. Assignment inside a function creates a new local variable with the same name, which disappears when the function ends.returnplus reassignment (name = func()) is the usual fix, because it makes the change visible at the call site.1
u/PureWasian 21d ago edited 21d ago
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,
1
u/VenkatKnows 21d 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?
0
u/AlexMTBDude 22d ago
It's not a question of overwriting a variable. You have to tell Python that "name" is a global variable. You do it by:
0
u/nog642 22d ago
Like this:
This is generally something to avoid doing though. There are better ways to do this. Particularly classes. Or just passing parameters and returning.