Python 模塊屬性:名稱、文檔、文件、字典

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 13:30
下一篇 2024-12-12 13:30

相關推薦

  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python函數名稱相同參數不同:多態

    Python是一門面向對象的編程語言,它強烈支持多態性 一、什麼是多態多態是面向對象三大特性中的一種,它指的是:相同的函數名稱可以有不同的實現方式。也就是說,不同的對象調用同名方法…

    編程 2025-04-29
  • Python中取出字典中對應鍵的值

    如何使用Python在字典中獲取特定鍵的值?這是Python編程中必須掌握的技能之一。本文將通過多個方面來詳細講解Python如何取出字典中對應鍵的值。 一、通過鍵名獲取值 當我們…

    編程 2025-04-29
  • 光模塊異常,SFP未認證(entityphysicalindex=6743835)——解決方案和

    如果您遇到類似optical module exception, sfp is not certified. (entityphysicalindex=6743835)的問題,那麼…

    編程 2025-04-29
  • Python如何遍歷字典中的key和value

    本文將詳細講解Python中如何遍歷字典中的key和value,包括多種遍歷方式以及在遍歷過程中的一些應用場景。 一、遍歷字典中的key和value 在Python中,字典是一種無…

    編程 2025-04-29
  • Vant ContactList 增加屬性的實現方法

    在使用前端UI框架Vant中的ContactList組件時,我們有時需要為此組件增加一些個性化的屬性,來滿足我們特定的需求。那麼,如何實現ContactList組件的增加屬性功能呢…

    編程 2025-04-29
  • 全面解讀數據屬性r/w

    數據屬性r/w是指數據屬性的可讀/可寫性,它在程序設計中扮演着非常重要的角色。下面我們從多個方面對數據屬性r/w進行詳細的闡述。 一、r/w的概念 數據屬性r/w即指數據屬性的可讀…

    編程 2025-04-29
  • Python模塊下載與安裝指南

    如果想要擴展Python的功能,可以使用Python模塊來實現。但是,在使用之前,需要先下載並安裝對應的模塊。本文將從以下多個方面對Python模塊下載與安裝進行詳細的闡述,包括使…

    編程 2025-04-29
  • Python編程三劍客——模塊、包、庫

    本文主要介紹Python編程三劍客:模塊、包、庫的概念、特點、用法,以及在實際編程中的實際應用,旨在幫助讀者更好地理解和應用Python編程。 一、模塊 1、概念:Python模塊…

    編程 2025-04-29
  • 如何使用pip安裝模塊

    pip作為Python默認的包管理系統,是安裝和管理Python包的一種方式,它可以輕鬆快捷地安裝、卸載和管理Python的擴展庫、模塊等。下面從幾個方面詳細介紹pip的使用方法。…

    編程 2025-04-28

發表回復

登錄後才能評論