Python中的的自省用法-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)Python中的的自省用法,文章內(nèi)容質(zhì)量較高,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)成立與2013年,公司以成都網(wǎng)站設(shè)計、網(wǎng)站制作、系統(tǒng)開發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計等為主要業(yè)務(wù),適用行業(yè)近百種。服務(wù)企業(yè)客戶成百上千,涉及國內(nèi)多個省份客戶。擁有多年網(wǎng)站建設(shè)開發(fā)經(jīng)驗。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計、宣傳推廣等服務(wù)。 通過專業(yè)的設(shè)計、獨特的風格,為不同客戶提供各種風格的特色服務(wù)。

什么是自???

在日常生活中,自?。╥ntrospection)是一種自我檢查行為。

在計算機編程中,自省是指這種能力:檢查某些事物以確定它是什么、它知道什么以及它能做什么。自省向程序員提供了極大的靈活性和控制力。

說的更簡單直白一點:自省就是面向?qū)ο蟮恼Z言所寫的程序在運行時,能夠知道對象的類型。簡單一句就是,運行時能夠獲知對象的類型。

例如python, buby, object-C, c++都有自省的能力,這里面的c++的自省的能力最弱,只能夠知道是什么類型,而像python可以知道是什么類型,還有什么屬性。

最好的理解自省就是通過例子: Type introspection 這里是各種編程語言中自?。╥ntrospection)的例子(這個鏈接里的例子很重要,也許你很難通過敘述理解什么是introspection,但是通過這些例子,一下子你就可以理解了)

回到Python,Python中比較常見的自?。╥ntrospection)機制(函數(shù)用法)有: dir(),type(), hasattr(), isinstance(),通過這些函數(shù),我們能夠在程序運行時得知對象的類型,判斷對象是否存在某個屬性,訪問對象的屬性。

dir()

dir() 函數(shù)可能是 Python 自省機制中最著名的部分了。它返回傳遞給它的任何對象的屬性名稱經(jīng)過排序的列表。如果不指定對象,則 dir() 返回當前作用域中的名稱。讓我們將 dir() 函數(shù)應(yīng)用于 keyword 模塊,并觀察它揭示了什么:

>>> import keyword
>>> dir(keyword)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'iskeyword', 'kwlist', 'main']

type()

type() 函數(shù)有助于我們確定對象是字符串還是整數(shù),或是其它類型的對象。它通過返回類型對象來做到這一點,可以將這個類型對象與 types 模塊中定義的類型相比較:

>>> type(42)<class 'int'>
>>> type([])<class 'list'>

isinstance()

可以使用 isinstance() 函數(shù)測試對象,以確定它是否是某個特定類型或定制類的實例:

>>> isinstance("python", str)
True

Python自省中help用法擴展:

打開python的IDLE,就進入到了python解釋器中,python解釋器本身是被認為是一個主模塊,然后在解釋器提示符>>>下輸入一些我們想了解的信息,所以首先我們會先尋求幫助,所以輸入help,接著輸入help(),我們就進入了help utility,然后循著提示keywords,modules,以了解python的關(guān)鍵字以及python自帶的或者我們額外安裝和定義的模塊,如果要退出,輸入'q',然后回車。

如果我們想了解某個對象(python里面所有對象都可以認為是對象),也可以求助也help(),不過要在括號里輸入對象的名稱,格式help(object),例如help(print),鑒于對象的自省內(nèi)容太多,有的只粘貼出部分內(nèi)容。

>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help()

Welcome to Python 3.6's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.6/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
...
help> keywords

Here is a list of the Python keywords. Enter any keyword to get more help.

False        def         if         raise
None        del         import       return
True        elif        in         try
and         else        is         while
as         except       lambda       with
assert       finally       nonlocal      yield
break        for         not         
class        from        or         
continue      global       pass        

help> modules

Please wait a moment while I gather a list of all available modules...

PIL         base64       idlelib       runpy
__future__     bdb         idna        runscript
__main__      binascii      idna_ssl      sched
_ast        binhex       imaplib       scrolledlist
_asyncio      bisect       imghdr       search
_bisect       browser       imp         
...
Enter any module name to get more help. Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".
>>> help('print')
Help on built-in function print in module builtins:

print(...)
  print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  
  Prints the values to a stream, or to sys.stdout by default.
  Optional keyword arguments:
  file: a file-like object (stream); defaults to the current sys.stdout.
  sep:  string inserted between values, default a space.
  end:  string appended after the last value, default a newline.
  flush: whether to forcibly flush the stream.

以上就是Python中的的自省用法,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容或?qū)W會更多技能,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊,感謝各位的閱讀。

網(wǎng)站名稱:Python中的的自省用法-創(chuàng)新互聯(lián)
鏈接地址:http://www.muchs.cn/article46/dpcjeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、電子商務(wù)、網(wǎng)站導(dǎo)航、網(wǎng)站維護、定制開發(fā)、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)