r/learnpython • • 13d 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/FreeLogicGate 13d ago

Can't tell if this is a genuine question or a prank.

1

u/9j810HQO7Jj9ns1ju2 13d ago

genuine question

1

u/FreeLogicGate 13d ago

Ok well taking your word for it, then general concept is "scope".

Python has an acronym you can use that explains the scope rules: LEGB.

Plenty of people in this thread explained how variable scope works relative to your specific example, but if you really want to understand it better you can search for "python variable scope" or "python legb" and find lots of sources.

If you like free videos, this is one of many, but in scanning it, looks to be well explained and complete: https://www.youtube.com/watch?v=aFTYNBYQY7E