什么是python的自省
什么是自?。?/strong>
在日常生活中,自省(introspection)是一種自我檢查行為。
在計(jì)算機(jī)編程中,自省是指這種能力:檢查某些事物以確定它是什么、它知道什么以及它能做什么。自省向程序員提供了極大的靈活性和控制力。
說(shuō)的更簡(jiǎn)單直白一點(diǎn):自省就是面向?qū)ο蟮恼Z(yǔ)言所寫(xiě)的程序在運(yùn)行時(shí),能夠知道對(duì)象的類型。簡(jiǎn)單一句就是,運(yùn)行時(shí)能夠獲知對(duì)象的類型。
例如python, buby, object-C, c++都有自省的能力,這里面的c++的自省的能力最弱,只能夠知道是什么類型,而像python可以知道是什么類型,還有什么屬性。
最好的理解自省就是通過(guò)例子: Type introspection 這里是各種編程語(yǔ)言中自?。╥ntrospection)的例子(這個(gè)鏈接里的例子很重要,也許你很難通過(guò)敘述理解什么是introspection,但是通過(guò)這些例子,一下子你就可以理解了)
回到Python,Python中比較常見(jiàn)的自?。╥ntrospection)機(jī)制(函數(shù)用法)有: dir(),type(), hasattr(), isinstance(),通過(guò)這些函數(shù),我們能夠在程序運(yùn)行時(shí)得知對(duì)象的類型,判斷對(duì)象是否存在某個(gè)屬性,訪問(wèn)對(duì)象的屬性。
dir()
dir() 函數(shù)可能是 Python 自省機(jī)制中最著名的部分了。它返回傳遞給它的任何對(duì)象的屬性名稱經(jīng)過(guò)排序的列表。如果不指定對(duì)象,則 dir() 返回當(dāng)前作用域中的名稱。讓我們將 dir() 函數(shù)應(yīng)用于 keyword 模塊,并觀察它揭示了什么:
>>> import keyword >>> dir(keyword) ['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'iskeyword', 'kwlist', 'main']
type()
type() 函數(shù)有助于我們確定對(duì)象是字符串還是整數(shù),或是其它類型的對(duì)象。它通過(guò)返回類型對(duì)象來(lái)做到這一點(diǎn),可以將這個(gè)類型對(duì)象與 types 模塊中定義的類型相比較:
>>> type(42)<class 'int'> >>> type([])<class 'list'>
isinstance()
可以使用 isinstance() 函數(shù)測(cè)試對(duì)象,以確定它是否是某個(gè)特定類型或定制類的實(shí)例:
>>> isinstance("python", str)
True
python自省中help用法擴(kuò)展:
打開(kāi)python的IDLE,就進(jìn)入到了python解釋器中,python解釋器本身是被認(rèn)為是一個(gè)主模塊,然后在解釋器提示符>>>下輸入一些我們想了解的信息,所以首先我們會(huì)先尋求幫助,所以輸入help,接著輸入help(),我們就進(jìn)入了help utility,然后循著提示keywords,modules,以了解python的關(guān)鍵字以及python自帶的或者我們額外安裝和定義的模塊,如果要退出,輸入'q',然后回車。
如果我們想了解某個(gè)對(duì)象(python里面所有對(duì)象都可以認(rèn)為是對(duì)象),也可以求助也help(),不過(guò)要在括號(hào)里輸入對(duì)象的名稱,格式help(object),例如help(print),鑒于對(duì)象的自省內(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.
到此這篇關(guān)于什么是python的自省的文章就介紹到這了,更多相關(guān)python自省是什么內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)的樸素貝葉斯算法經(jīng)典示例【測(cè)試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)的樸素貝葉斯算法,結(jié)合實(shí)例形式詳細(xì)分析了Python實(shí)現(xiàn)與使用樸素貝葉斯算法的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
python中偏函數(shù)partial用法實(shí)例分析
這篇文章主要介紹了python中偏函數(shù)partial用法,實(shí)例分析了偏函數(shù)partial的功能、定義及使用方法,需要的朋友可以參考下2015-07-07
關(guān)于使用Python的time庫(kù)制作進(jìn)度條程序
這篇文章主要介紹了關(guān)于使用Python的time庫(kù)制作進(jìn)度條程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
基于python實(shí)現(xiàn)分析識(shí)別文章/內(nèi)容中的高頻詞和關(guān)鍵詞
要分析一篇文章的高頻詞和關(guān)鍵詞,可以使用 Python 中的 nltk 庫(kù)和 collections 庫(kù)或者jieba庫(kù)來(lái)實(shí)現(xiàn),本篇文章介紹基于兩種庫(kù)分別實(shí)現(xiàn)分析內(nèi)容中的高頻詞和關(guān)鍵詞,需要的朋友可以參考下2023-09-09
python smtplib發(fā)送多個(gè)email聯(lián)系人的實(shí)現(xiàn)
這篇文章主要介紹了python smtplib發(fā)送多個(gè)email聯(lián)系人的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
python爬蟲(chóng)學(xué)習(xí)筆記之Beautifulsoup模塊用法詳解
這篇文章主要介紹了python爬蟲(chóng)學(xué)習(xí)筆記之Beautifulsoup模塊用法,結(jié)合實(shí)例形式詳細(xì)分析了python爬蟲(chóng)Beautifulsoup模塊基本功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
Python接口傳輸url與flask數(shù)據(jù)詳解
這篇文章主要介紹了Python通過(guò)接口傳輸url與flask數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09

