共計 252 個字符,預計需要花費 1 分鐘才能閱讀完成。
在 Python 中,可以使用 global
關鍵字來定義全局變量。全局變量是在函數外部定義的變量,在整個程序中都可以訪問到。
以下是定義全局變量的方法:
- 在函數外部直接定義全局變量:
global_variable = 10
- 在函數內部使用
global
關鍵字聲明一個變量為全局變量:
def my_function():
global global_variable
global_variable = 10
在這兩種情況下,global_variable
都是全局變量,可以在整個程序中的任何位置訪問到。
丸趣 TV 網 – 提供最優質的資源集合!
正文完