Python 模塊有描述它的屬性。屬性執行一些任務或包含一些關於模塊的信息。一些重要屬性解釋如下:
_ 名稱 _ 屬性
__name__
屬性返回模塊的名稱。默認情況下,文件的名稱(不包括擴展名。py)是 name 屬性的值。
Example: name Attribute
>>> import math
>>> math.__name__
'math'
同樣,它給出了自定義模塊的名稱。
Example: name Attribute
>>> hello.__name__
'hello'
但是,這可以通過為該屬性分配不同的字符串來修改。如下圖改變hello.py
。
Example: Set name
def SayHello(name):
print ("Hi {}! How are you?".format(name))
__name__="SayHello"
現在檢查__name__
屬性。
>>> import hello
>>> hello.__name__
'SayHello'
__name__
屬性的值是 Python 交互 Shell上的__main__
。
>>> __name__
'__main__'
當我們運行任何 Python 腳本(即模塊)時,其__name__
屬性也被設置為__main__
。 例如,在 IDLE 中創建以下 welcome.py。
Example: welcome.py
print("__name__ = ", __name__)
按 F5 在 IDLE 中運行上述 welcome.py。您將看到以下結果。
Output in IDLE:
>>> __name__ = __main__
但是,當導入該模塊時,其__name__
被設置為其文件名。 現在,用以下內容導入新文件 test.py 中的歡迎模塊。
Example: test.py
import welcome
print("__name__ = ", __name__)
現在按 F5 在 IDLE 中運行 test.py。__name__
屬性現在是「歡迎」。
Example: test.py
__name__ = welcome
該屬性允許 Python 腳本用作可執行文件或模塊。
更多信息請訪問 Python 中的 main。
_ 文檔 _ 屬性
doc 屬性表示用模塊代碼編寫的文檔字符串(docstring)行。
Example:
>>> import math
>>> math.__doc__
'This module is always available. It provides access to the mathematical functions defined by the C standard.'
考慮以下腳本保存為test.py
模塊。
test.py
"""This is docstring of test module"""
def SayHello(name):
print ("Hi {}! How are you?".format(name))
return
__doc__
屬性將返回一個在模塊代碼開頭定義的字符串。
Example:
>>> import test
>>> test.__doc__
'This is docstring of test module'
_ 文件 _ 屬性
__file__
是一個可選屬性,保存從中加載它的模塊文件的名稱和路徑。
Example: file Attribute
>>> import io
>>> io.__file__
'C:\\python37\\lib\\io.py'
_ 字典 _ 屬性
__dict__
屬性將返回模塊屬性、函數和其他定義及其各自值的字典對象。
Example: dict Attribute
>>> import math
>>> math.__dict__
{'__name__': 'math', '__doc__': 'This module is always available. It provides a
ccess to the\nmathematical functions defined by the C standard.', '__package__':
'', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': Modu
leSpec(name='math', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='
built-in'), 'acos': <built-in function acos>, 'acosh': <built-in function acosh>
, 'asin': <built-in function asin>, 'asinh': <built-in function asinh>, 'atan':
<built-in function atan>, 'atan2': <built-in function atan2>, 'atanh': <built-in
function atanh>, 'ceil': <built-in function ceil>, 'copysign': <built-in functi
on copysign>, 'cos': <built-in function cos>, 'cosh': <built-in function cosh>,
'degrees': <built-in function degrees>, 'erf': <built-in function erf>, 'erfc':
<built-in function erfc>, 'exp': <built-in function exp>, 'expm1': <built-in fun
ction expm1>, 'fabs': <built-in function fabs>, 'factorial': <built-in function
factorial>, 'floor': <built-in function floor>, 'fmod': <built-in function fmod>
, 'frexp': <built-in function frexp>, 'fsum': <built-in function fsum>, 'gamma':
<built-in function gamma>, 'gcd': <built-in function gcd>, 'hypot': <built-in f
unction hypot>, 'isclose': <built-in function isclose>, 'isfinite': <built-in fu
nction isfinite>, 'isinf': <built-in function isinf>, 'isnan': <built-in functio
n isnan>, 'ldexp': <built-in function ldexp>, 'lgamma': <built-in function lgamm
a>, 'log': <built-in function log>, 'log1p': <built-in function log1p>, 'log10':
<built-in function log10>, 'log2': <built-in function log2>, 'modf': <built-in
function modf>, 'pow': <built-in function pow>, 'radians': <built-in function ra
dians>, 'remainder': <built-in function remainder>, 'sin': <built-in function si
n>, 'sinh': <built-in function sinh>, 'sqrt': <built-in function sqrt>, 'tan': <
built-in function tan>, 'tanh': <built-in function tanh>, 'trunc': <built-in fun
ction trunc>, 'pi': 3.141592653589793, 'e': 2.718281828459045, 'tau': 6.28318530
7179586, 'inf': inf, 'nan': nan}
dir() 是一個內置函數,也返回一個模塊中所有屬性和函數的列表。
Know Module Attributes and Methods
在 Python 文檔中了解更多模塊屬性。***
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248968.html