日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 全局_全局关键字,带Python示例

發布時間:2023/12/1 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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_name

Note: 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示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。