Skip to main content

Python 关键字 -- `global`

MarshioLess than 1 minutepython

key wordsopen in new window

用法

# 预先全局声明
current_date = datetime.datetime.now().strftime('%Y-%m-%d')

def a():
    global current_date
    current_date = '2024-09-05'
    
if __name__ == '__main__':
    print(current_date)
    # 输出 2024-09-03
    a()
    print(current_date)
    # 输出 2024-09-05