📜  python 在函数中编辑全局变量 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:11.026000             🧑  作者: Mango

代码示例1
globalvar = "flower"

def editglobalvar():
    global globalvar # accesses the "globalvar" variable, so when we change it
    # it won't assign the new value to a local variable,
    # not changing the value of the global variable
    
    globalvar = "good" # assigning new value
    
print(globalvar) # outputs "flower"
# if we didnt use the "global" keyword in the function, it would print out 
# "flower"