Python中if_name__ == "__main__"詳しく説明する
9877 ワード
Python初心者としてif_に遭遇することが多いname__ == "__main__"この問題は、次にその役割を説明します.
例えばtestを作成しますpyファイル、1つのpythonファイルはpythonのモジュールと見なすことができ、このpythonモジュール(.pyファイル)には2つの使用方法があります.直接実行使用とモジュールとして他のモジュールに呼び出されます.
説明する_name__:各モジュールには、組み込み属性があります.name__.そして_name__の値はpythonモジュール(.pyファイル)の使い方によって異なります.直接実行して使用する場合、このモジュールの__name_値は「_main_」;モジュールとして他のモジュールによって呼び出された場合、このモジュール(.pyファイル)の__name_値は、パスおよびファイル拡張子を持たずにモジュール(.pyファイル)のファイル名です.
例えば『Pythonコアプログラミング』第2版第5章答えの5-8題です.
このコードでファイルが直接実行されると、バックグラウンドに「**Directexecute*」と印刷されます.
他のモジュールによって呼び出された場合は、バックグラウンドに*****Module called*****と印刷されます.
私たちは2つの方法でtestを実行します.pyファイル:
1つ目はtestを直接実行することです.pyファイル.
linux端末で直接実行
linux端末では「*****Direct execute*****」と印刷され、モジュール(.pyファイル)が直接実行されていることを証明します.モジュールの__name__値は「_main_」です.
2つ目は、他のモジュールによって呼び出されます.
python環境に入り、import testファイルモジュール.
linux端末では、「*****Module called*****」のほかに、パスとファイル拡張子を持たずにモジュールのファイル名であるモジュール(.pyファイル)が呼び出されたことを証明するモジュール(.pyファイル)が印刷されていることがわかります.
例えばtestを作成しますpyファイル、1つのpythonファイルはpythonのモジュールと見なすことができ、このpythonモジュール(.pyファイル)には2つの使用方法があります.直接実行使用とモジュールとして他のモジュールに呼び出されます.
説明する_name__:各モジュールには、組み込み属性があります.name__.そして_name__の値はpythonモジュール(.pyファイル)の使い方によって異なります.直接実行して使用する場合、このモジュールの__name_値は「_main_」;モジュールとして他のモジュールによって呼び出された場合、このモジュール(.pyファイル)の__name_値は、パスおよびファイル拡張子を持たずにモジュール(.pyファイル)のファイル名です.
例えば『Pythonコアプログラミング』第2版第5章答えの5-8題です.
このコードでファイルが直接実行されると、バックグラウンドに「**Directexecute*」と印刷されます.
他のモジュールによって呼び出された場合は、バックグラウンドに*****Module called*****と印刷されます.
1 #test.py
2
3 #!/usr/bin/python
4
5 from math import pi
6
7 def square(length):
8 area = length ** 2
9 print "The area of square is %0.2f" %area
10
11 def cube(length):
12 volume = length ** 3
13 print "The volume of cube is %0.2f" %volume
14
15 def circle(radius):
16 area = pi * radius ** 2
17 print "The area of circle is %0.2f" %area
18
19 def sphere(radius):
20 volume = 4 * pi * radius ** 2
21 print "The volume of sphere is %0.2f" %volume
22
23 if __name__ == "__main__":
24 try:
25 print "*****Direct execute*****"
26 num = float(raw_input("Enter a num:"))
27 square(num)
28 cube(num)
29 circle(num)
30 sphere(num)
31 except ValueError, e:
32 print " Input a invaild num !"
33
34 if __name__ == "test":
35 try:
36 print "*****Module called*****"
37 num = float(raw_input("Enter a num:"))
38 square(num)
39 cube(num)
40 circle(num)
41 sphere(num)
42 except ValueError, e:
43 print " Input a invaild num !"
私たちは2つの方法でtestを実行します.pyファイル:
1つ目はtestを直接実行することです.pyファイル.
linux端末で直接実行
[Feng@A005 ~]$ ./test.py
*****Direct execute*****
Enter a num:5
The area of square is 25.00
The volume of cube is 125.00
The area of circle is 78.54
The volume of sphere is 314.16
linux端末では「*****Direct execute*****」と印刷され、モジュール(.pyファイル)が直接実行されていることを証明します.モジュールの__name__値は「_main_」です.
2つ目は、他のモジュールによって呼び出されます.
python環境に入り、import testファイルモジュール.
[Feng@A005 ~]$ python
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
*****Module called*****
Enter a num:5
The area of square is 25.00
The volume of cube is 125.00
The area of circle is 78.54
The volume of sphere is 314.16
linux端末では、「*****Module called*****」のほかに、パスとファイル拡張子を持たずにモジュールのファイル名であるモジュール(.pyファイル)が呼び出されたことを証明するモジュール(.pyファイル)が印刷されていることがわかります.