用 C 族語言(C、C++、Java、C# 等)編寫的程序。)需要main()功能來指示執行的起點。
另一方面,在 Python 中,沒有main()函數的概念,因為它是一種基於解釋器的語言,同樣可以在交互 Shell中使用。 擴展名為.py的 Python 程序文件包含多個語句。Python 程序文件的執行從第一條語句開始。
Python 包含名為__name__的特殊變量,該變量包含作為字符串執行的代碼的範圍。__main__是頂層代碼執行的頂層作用域的名稱。
例如,解釋器 Shell 中執行的代碼的範圍將是__main__,如下所示。
Python Shell
>>>__name__
'__main__' 所有的功能和模塊都將在解釋器 Shell 的頂層範圍__main___內執行。
Python Shell
>>> def f1():
print(__name__)
>>> f1() 甚至內部功能都是在頂層範圍__main__內執行的:
Python Shell
>>> def f1():
print(__name__)
def f2():
print(__name__)
f2()
>>> f1()
__main__
__main__ 一個 Python 文件可以包含多個可以獨立執行的函數和語句。例如,考慮以下addition.py:
addition.py
def add(x,y):
z=x+y
print('add() executed under the scope: ', __name__)
return z
x=input('Enter the first number to add: ')
y=input('Enter the secode number to add: ')
result = add(int(x),int(y))
print(x, '+', y,'=', result)
print('Code executed under the scope: ', __name__) Python 程序文件可以通過以下方式執行:
- 使用命令提示符/終端將 Python 文件作為腳本執行。
- 使用 Import 語句將 Python 代碼從一個文件導入到另一個文件
C:\Python37> python addition.py
Enter the first number to add: 3
Enter the secode number to add: 3
add() executed under the scope: __main__
3 + 3 = 6
Code executed under the scope: __main__可以看到,頂層範圍__main__下執行的addition.py。
addition.py文件可以作為模塊在另一個文件中使用,也可以通過導入在交互 Shell 中使用。
讓我們看看當你在交互 Shell 中導入addition模塊時會發生什麼。
Python Shell
>>> import addition
Enter the first number to add: 3
Enter the secode number to add: 3
add() executed under the scope: addition
3 + 3 = 6
Code executed under the scope: addition 上面,導入語句從第一條語句開始執行。但是,我們只想使用add()方法,不想執行其他語句。
這裡我們可以使用特殊變量__name__來檢查addition.py文件的作用域和執行語句,只有當它從命令提示符/終端獨立執行時,而不是當它被導入到其他文件/模塊中時。 重寫addition.py,如下圖。
addition.py
def add(x, y):
z=x+y
print('add() executed under the scope: ', __name__)
return z
if __name__ == '__main__':
x=input('Enter the first number to add: ')
y=input('Enter the secode number to add: ')
result = add(int(x),int(y))
print(x, '+', y,'=', result)
print('Code executed under the scope: ', __name__) 以上,if 條件檢查如果範圍是__main__,那麼只執行接受用戶輸入並添加它們的代碼。
現在,讓我們看看當我們在交互 Shell 中導入上面的addition模塊時會發生什麼。
Python Shell
>>> import addition
>>> addition.add(3,3)
add() executed under the scope: addition
6 也可以使用from import語句,如下所示:
Python Shell
>>> from addition import add
>>> add(3,3)
add() executed under the scope: addition
6 如您所見,因為我們使用了一個 if 條件來檢查作用域,所以它在導入addition模塊後不會執行用戶輸入的代碼,因為它是在模塊的作用域下執行的,也就是addition作用域。 只進口add()法。在其他模塊中導入addition模塊也會發生同樣的情況。
現在,讓我們看看當您從命令提示符/終端執行它時會發生什麼。
C:\Python37> python addition.py
Enter the first number to add: 3
Enter the secode number to add: 3
add() executed under the scope: __main__
3 + 3 = 6
Code executed under the scope: __main__可以看到,由於addition.py是在頂級範圍__main__內執行的,所以還是執行同樣的代碼。
因此,name的值允許 Python 解釋器確定模塊是否是可執行腳本。如果其值為main,將執行函數定義之外的語句。如果沒有,模塊的內容將被填充到頂層模塊(或解釋器名稱空間)中,而不包含可執行部分。
注意:從命令提示符/終端執行的 Python 腳本文件將在頂層作用域__main__作用域下執行。但是,導入模塊將在模塊自己的範圍內執行。因此,頂層範圍將是__main__,第二個範圍將是模塊的範圍。
因此,使用特殊變量__name__和頂級範圍__main__增加了可重用性。Python 腳本文件可以作為獨立腳本從命令提示符/終端執行,也可以作為模塊導入。****
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/129249.html
微信掃一掃
支付寶掃一掃