python 全局_全局关键字,带Python示例
python 全局
Python全局關鍵字 (Python global keyword)
global is a keyword (case-sensitive) in python, it is used to declare a global variable inside a function (from a non-global scope).
global是python中的一個關鍵字(區分大小寫),用于在函數內部聲明一個全局變量(來自非全局范圍)。
As we know that variables are accessible within the same scope in which they are declared but a global variable can be accessed within a program anywhere. In python, we can define a global variable inside a function or in a non-global using by using global keyword.
眾所周知,在聲明變量的相同范圍內可以訪問變量,但是可以在程序中的任何位置訪問全局變量 。 在python中,我們可以通過使用global關鍵字定義函數內部或非全局變量中的全局變量。
Syntax of global keyword
全局關鍵字的語法
global variable_nameNote: Before accessing the global variable outside of the function, function in which global variable is declared must be called.
注意:在訪問函數外部的全局變量之前,必須先調用聲明了全局變量的函數。
Example:
例:
# functiondef myfunc():# global variableglobal a# assigning the value to aa = 10# main code# function call myfunc()print("outside the function a: ", a)Output:outside the function a: 10最終關鍵字的Python示例 (Python examples of finally keyword)
Example 1: Declare a global variable inside a function, assign the value after the declaration, and print the value inside and outside of the function.
示例1:在函數內部聲明一個全局變量,在聲明之后分配值,并在函數內部和外部打印該值。
# python code to demonstrate example of # gloabl keyword # function def myfunc():# global variableglobal a# assigning the value to aa = 10# printing the valueprint("inside myfunc() a: ", a)# main code # function call myfunc() print("outside the function a: ", a)Output
輸出量
inside myfunc() a: 10 outside the function a: 10翻譯自: https://www.includehelp.com/python/global-keyword-with-example.aspx
python 全局
總結
以上是生活随笔為你收集整理的python 全局_全局关键字,带Python示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中dict函数_dict()
- 下一篇: 租个电脑一个月多少钱啊?