函数内部无法修改函数及跨函数修改state变量未生效问题求助
state Variable Issue Hey there, sorry you've been stuck on this for 3 hours—let's get this sorted out! The problem you're seeing is super common, and it almost always boils down to variable scope or how you're referencing/modifying the original state variable from game.py.
Why This Is Happening
When you modify state inside fun3 and see the updated value there, but func1 still shows the original "menu" value, you're almost certainly modifying a local copy of state instead of the original variable in game.py. Here are the two most likely scenarios:
- You imported
statedirectly (likefrom game import state) instead of importing the whole module, so you're working with a local reference that doesn't link back to the original. - You didn't declare
stateas a global variable (if it's in the same file) or you're passing it as an argument tofun3without accounting for immutable types (like strings, which don't get modified in-place).
Fixes to Try
1. Import the Entire game Module (Instead of the Variable Directly)
If you're using from game import state, you're creating a local copy of the variable in your current file. Modifying this copy won't affect the original in game.py. Instead:
# Import the whole module import game def fun3(): # Modify the variable directly through the module reference game.state = "game" print(game.state) # Will show "game" def func1(): fun3() print(game.state) # Now shows "game" instead of "menu"
2. Use global Declaration (If state Is in the Same File)
If state is defined in the same file as fun3 and func1, you need to tell Python you want to modify the global variable, not create a local one:
state = "menu" def fun3(): # Declare that we're using the global state variable global state state = "game" print(state) # Shows "game" def func1(): fun3() print(state) # Now shows "game"
3. Wrap state in a Mutable Container (For Function Arguments)
If you're passing state as an argument to fun3, immutable types like strings can't be modified in-place. Instead, put state inside a mutable object (like a dictionary) so changes affect the original:
# game.py state = {"current": "menu"} # In your other functions def fun3(state_dict): state_dict["current"] = "game" print(state_dict["current"]) # Shows "game" def func1(): fun3(state) print(state["current"]) # Shows "game"
4. Return the Updated Value and Reassign
If you want to avoid globals or mutable containers, have fun3 return the updated state and reassign it wherever you call fun3:
# game.py state = "menu" def fun3(current_state): new_state = "game" print(new_state) # Shows "game" return new_state def func1(): global state # Or use game.state if importing the module state = fun3(state) print(state) # Shows "game"
Quick Check to Verify
Before trying any fixes, add a print statement for id(state) in both fun3 and func1. If the IDs are different, you're dealing with two separate variables—this confirms you're modifying a local copy instead of the original.
内容的提问来源于stack exchange,提问作者Abdul Rafey

