inspect
--- 檢查對(duì)象?
源代碼: Lib/inspect.py
inspect
模塊提供了一些有用的函數(shù)幫助獲取對(duì)象的信息,例如模塊、類(lèi)、方法、函數(shù)、回溯、幀對(duì)象以及代碼對(duì)象。例如它可以幫助你檢查類(lèi)的內(nèi)容,獲取某個(gè)方法的源代碼,取得并格式化某個(gè)函數(shù)的參數(shù)列表,或者獲取你需要顯示的回溯的詳細(xì)信息。
該模塊提供了4種主要的功能:類(lèi)型檢查、獲取源代碼、檢查類(lèi)與函數(shù)、檢查解釋器的調(diào)用堆棧。
類(lèi)型和成員?
getmembers()
函數(shù)獲取對(duì)象的成員,例如類(lèi)或模塊。函數(shù)名以"is"開(kāi)始的函數(shù)主要作為 getmembers()
的第2個(gè)參數(shù)使用。它們也可用于判定某對(duì)象是否有如下的特殊屬性:
類(lèi)型 |
屬性 |
描述 |
---|---|---|
module -- 模塊 |
__doc__ |
文檔字符串 |
__file__ |
文件名(內(nèi)置模塊沒(méi)有文件名) |
|
class -- 類(lèi) |
__doc__ |
文檔字符串 |
__name__ |
類(lèi)定義時(shí)所使用的名稱(chēng) |
|
__qualname__ |
qualified name -- 限定名稱(chēng) |
|
__module__ |
該類(lèi)型被定義時(shí)所在的模塊的名稱(chēng) |
|
method -- 方法 |
__doc__ |
文檔字符串 |
__name__ |
該方法定義時(shí)所使用的名稱(chēng) |
|
__qualname__ |
qualified name -- 限定名稱(chēng) |
|
__func__ |
實(shí)現(xiàn)該方法的函數(shù)對(duì)象 |
|
__self__ |
該方法被綁定的實(shí)例,若沒(méi)有綁定則為 |
|
__module__ |
定義此方法的模塊的名稱(chēng) |
|
function -- 函數(shù) |
__doc__ |
文檔字符串 |
__name__ |
用于定義此函數(shù)的名稱(chēng) |
|
__qualname__ |
qualified name -- 限定名稱(chēng) |
|
__code__ |
包含已編譯函數(shù)的代碼對(duì)象 bytecode |
|
__defaults__ |
所有位置或關(guān)鍵字參數(shù)的默認(rèn)值的元組 |
|
__kwdefaults__ |
保存僅關(guān)鍵字參數(shù)的所有默認(rèn)值的映射 |
|
__globals__ |
此函數(shù)定義所在的全局命名空間 |
|
__builtins__ |
builtins 命名空間 |
|
__annotations__ |
參數(shù)名稱(chēng)到注解的映射;保留鍵 |
|
__module__ |
此函數(shù)定義所在的模塊名稱(chēng) |
|
traceback -- 回溯 |
tb_frame |
此層的幀對(duì)象 |
tb_lasti |
在字節(jié)碼中最后嘗試的指令的索引 |
|
tb_lineno |
當(dāng)前行在 Python 源代碼中的行號(hào) |
|
tb_next |
下一個(gè)內(nèi)部回溯對(duì)象(由本層調(diào)用) |
|
frame -- 幀 |
f_back |
下一個(gè)外部幀對(duì)象(此幀的調(diào)用者) |
f_builtins |
此幀執(zhí)行時(shí)所在的 builtins 命名空間 |
|
f_code |
在此幀中執(zhí)行的代碼對(duì)象 |
|
f_globals |
此幀執(zhí)行時(shí)所在的全局命名空間 |
|
f_lasti |
在字節(jié)碼中最后嘗試的指令的索引 |
|
f_lineno |
當(dāng)前行在 Python 源代碼中的行號(hào) |
|
f_locals |
此幀所看到的局部命名空間 |
|
f_trace |
此幀的追蹤函數(shù),或``None`` |
|
code -- 代碼 |
co_argcount |
參數(shù)數(shù)量(不包括僅關(guān)鍵字參數(shù)、* 或 ** 參數(shù)) |
co_code |
字符串形式的原始字節(jié)碼 |
|
co_cellvars |
單元變量名稱(chēng)的元組(通過(guò)包含作用域引用) |
|
co_consts |
字節(jié)碼中使用的常量元組 |
|
co_filename |
創(chuàng)建此代碼對(duì)象的文件的名稱(chēng) |
|
co_firstlineno |
第一行在Python源代碼中的行號(hào) |
|
co_flags |
|
|
co_lnotab |
編碼的行號(hào)到字節(jié)碼索引的映射 |
|
co_freevars |
自由變量的名字組成的元組(通過(guò)函數(shù)閉包引用) |
|
co_posonlyargcount |
僅限位置參數(shù)的數(shù)量 |
|
co_kwonlyargcount |
僅限關(guān)鍵字參數(shù)的數(shù)量(不包括 ** 參數(shù)) |
|
co_name |
定義此代碼對(duì)象的名稱(chēng) |
|
co_qualname |
fully-qualified name with which this code object was defined |
|
co_names |
tuple of names other than arguments and function locals |
|
co_nlocals |
局部變量的數(shù)量 |
|
co_stacksize |
需要虛擬機(jī)堆??臻g |
|
co_varnames |
參數(shù)名和局部變量的元組 |
|
generator -- 生成器 |
__name__ |
名稱(chēng) |
__qualname__ |
qualified name -- 限定名稱(chēng) |
|
gi_frame |
frame -- 幀 |
|
gi_running |
生成器在運(yùn)行嗎? |
|
gi_code |
code -- 代碼 |
|
gi_yieldfrom |
通過(guò)``yield from``迭代的對(duì)象,或``None`` |
|
coroutine -- 協(xié)程 |
__name__ |
名稱(chēng) |
__qualname__ |
qualified name -- 限定名稱(chēng) |
|
cr_await |
正在等待的對(duì)象,或``None`` |
|
cr_frame |
frame -- 幀 |
|
cr_running |
這個(gè)協(xié)程正在運(yùn)行嗎? |
|
cr_code |
code -- 代碼 |
|
cr_origin |
協(xié)程被創(chuàng)建的位置,或``None``。參見(jiàn) |
|
builtin |
__doc__ |
文檔字符串 |
__name__ |
此函數(shù)或方法的原始名稱(chēng) |
|
__qualname__ |
qualified name -- 限定名稱(chēng) |
|
__self__ |
方法綁定到的實(shí)例,或``None`` |
在 3.5 版更改: 為生成器添加``__qualname__``和``gi_yieldfrom``屬性。
生成器的``__name__``屬性現(xiàn)在由函數(shù)名稱(chēng)設(shè)置,而不是代碼對(duì)象名稱(chēng),并且現(xiàn)在可以被修改。
在 3.7 版更改: 為協(xié)程添加``cr_origin``屬性。
在 3.10 版更改: 為函數(shù)添加``__builtins__``屬性。
- inspect.getmembers(object[, predicate])?
返回一個(gè)對(duì)象上的所有成員,組成以
(名稱(chēng), 值)
對(duì)為元素的列表,按名稱(chēng)排序。如果提供了可選的 predicate 參數(shù)(會(huì)對(duì)每個(gè)成員的值
對(duì)象進(jìn)行一次調(diào)用),則僅包含該斷言為真的成員。備注
如果要讓
getmembers()
返回元類(lèi)中定義的類(lèi)屬性,那么實(shí)參須為一個(gè)類(lèi)且這些屬性在元類(lèi)的自定義方法__dir__()
中列出。
- inspect.getmembers_static(object[, predicate])?
Return all the members of an object in a list of
(name, value)
pairs sorted by name without triggering dynamic lookup via the descriptor protocol, __getattr__ or __getattribute__. Optionally, only return members that satisfy a given predicate.備注
getmembers_static()
may not be able to retrieve all members that getmembers can fetch (like dynamically created attributes) and may find members that getmembers can't (like descriptors that raise AttributeError). It can also return descriptor objects instead of instance members in some cases.3.11 新版功能.
- inspect.getmodulename(path)?
返回由文件名 path 表示的模塊名字,但不包括外層的包名。文件擴(kuò)展名會(huì)檢查是否在
importlib.machinery.all_suffixes()
列出的條目中。如果符合,則文件路徑的最后一個(gè)組成部分會(huì)去掉后綴名后被返回;否則返回``None``。值得注意的是,這個(gè)函數(shù) 僅 返回可以作為 Python 模塊的名字,而有可能指向一個(gè) Python 包的路徑仍然會(huì)返回``None``。
在 3.3 版更改: 該函數(shù)直接基于
importlib
。
- inspect.ismodule(object)?
當(dāng)該對(duì)象是一個(gè)模塊時(shí)返回``True``。
- inspect.isclass(object)?
當(dāng)該對(duì)象是一個(gè)類(lèi)時(shí)返回``True``,無(wú)論是內(nèi)置類(lèi)或者 Python 代碼中定義的類(lèi)。
- inspect.ismethod(object)?
當(dāng)該對(duì)象是一個(gè) Python 寫(xiě)成的綁定方法時(shí)返回``True``。
- inspect.isfunction(object)?
當(dāng)該對(duì)象是一個(gè) Python 函數(shù)時(shí)(包括使用 lambda 表達(dá)式創(chuàng)造的函數(shù)),返回``True``。
- inspect.isgeneratorfunction(object)?
當(dāng)該對(duì)象是一個(gè) Python 生成器函數(shù)時(shí)返回``True``。
在 3.8 版更改: 對(duì)于使用
functools.partial()
封裝的函數(shù),如果被封裝的函數(shù)是一個(gè) Python 生成器函數(shù),現(xiàn)在也會(huì)返回``True``。
- inspect.isgenerator(object)?
當(dāng)該對(duì)象是一個(gè)生成器時(shí)返回``True``。
- inspect.iscoroutinefunction(object)?
當(dāng)該對(duì)象是一個(gè) 協(xié)程函數(shù) (通過(guò)
async def
語(yǔ)法定義的函數(shù))時(shí)返回``True``。3.5 新版功能.
在 3.8 版更改: 對(duì)于使用
functools.partial()
封裝的函數(shù),如果被封裝的函數(shù)是一個(gè) 協(xié)程函數(shù) ,現(xiàn)在也會(huì)返回``True``。
- inspect.iscoroutine(object)?
當(dāng)該對(duì)象是一個(gè)由
async def
函數(shù)創(chuàng)建的 協(xié)程 時(shí)返回``True``。3.5 新版功能.
- inspect.isawaitable(object)?
如果該對(duì)象可以在
await
表達(dá)式中使用時(shí)返回``True``。也可以用于區(qū)分基于生成器的協(xié)程和常規(guī)的生成器:
def gen(): yield @types.coroutine def gen_coro(): yield assert not isawaitable(gen()) assert isawaitable(gen_coro())
3.5 新版功能.
- inspect.isasyncgenfunction(object)?
如果該對(duì)象是一個(gè) 異步生成器 函數(shù)則返回``True``,例如:
>>> async def agen(): ... yield 1 ... >>> inspect.isasyncgenfunction(agen) True
3.6 新版功能.
在 3.8 版更改: 對(duì)于使用
functools.partial()
封裝的函數(shù),如果被封裝的函數(shù)是一個(gè) 異步生成器 ,現(xiàn)在也會(huì)返回``True``。
- inspect.isasyncgen(object)?
如果該對(duì)象是一個(gè)由 異步生成器 函數(shù)創(chuàng)建的 異步生成器迭代器,則返回
True
。3.6 新版功能.
- inspect.istraceback(object)?
如果該對(duì)象是一個(gè)回溯則返回
True
。
- inspect.isframe(object)?
如果該對(duì)象是一個(gè)幀對(duì)象則返回
True
。
- inspect.iscode(object)?
如果該對(duì)象是一個(gè)代碼對(duì)象則返回
True
。
- inspect.isbuiltin(object)?
如果該對(duì)象是一個(gè)內(nèi)置函數(shù)或一個(gè)綁定的內(nèi)置方法,則返回
True
。
- inspect.ismethodwrapper(object)?
Return
True
if the type of object is aMethodWrapperType
.These are instances of
MethodWrapperType
, such as__str__()
,__eq__()
and__repr__()
- inspect.isroutine(object)?
如果該對(duì)象是一個(gè)用戶(hù)定義的或內(nèi)置的函數(shù)或者方法,則返回
True
。
- inspect.isabstract(object)?
如果該對(duì)象是一個(gè)抽象基類(lèi)則返回
True
。
- inspect.ismethoddescriptor(object)?
如果該對(duì)象是一個(gè)方法描述器,但
ismethod()
、isclass()
、isfunction()
及isbuiltin()
均不為真,則返回True
。例如,該函數(shù)對(duì)于
int.__add__
為真。一個(gè)通過(guò)此測(cè)試的對(duì)象可以有__get__()
方法,但不能有__set__()
方法,除此以外的屬性集合是可變的。一個(gè)__name__
屬性通常是合理的,而__doc__
屬性常常也是。即使是通過(guò)描述器實(shí)現(xiàn)的函數(shù),如果通過(guò)其他某一個(gè)測(cè)試,則
ismethoddescriptor()
測(cè)試則會(huì)返回False
。這單純是因?yàn)槠渌麥y(cè)試提供了更多保證。比如,當(dāng)一個(gè)對(duì)象通過(guò)ismethod()
測(cè)試時(shí),你可以使用__func__
等屬性。
- inspect.isdatadescriptor(object)?
如果該對(duì)象是一個(gè)數(shù)據(jù)描述器則返回
True
。數(shù)據(jù)描述器擁有
__set__
或__delete__
方法。比如屬性(通過(guò) Python 定義)、getset 和成員。后二者是通過(guò) C 定義的,并且也有對(duì)應(yīng)的更具體的測(cè)試,并且在不同的 Python 實(shí)現(xiàn)中也是健壯的。典型地,數(shù)據(jù)描述器會(huì)擁有__name__
和__doc__
屬性(屬性、getset 和成員都包含這兩個(gè)屬性),但這并無(wú)保證。
- inspect.isgetsetdescriptor(object)?
如果該對(duì)象是一個(gè) getset 描述器則返回
True
。CPython implementation detail: getset 是在擴(kuò)展模塊中通過(guò)
PyGetSetDef
結(jié)構(gòu)體定義的屬性。對(duì)于不包含這種類(lèi)型的 Python 實(shí)現(xiàn),這個(gè)方法將永遠(yuǎn)返回False
。
- inspect.ismemberdescriptor(object)?
如果該對(duì)象是一個(gè)成員描述器則返回
True
。CPython implementation detail: 成員描述器是在擴(kuò)展模塊中通過(guò)
PyMemberDef
結(jié)構(gòu)體定義的屬性。對(duì)于不包含這種類(lèi)型的 Python 實(shí)現(xiàn),這個(gè)方法將永遠(yuǎn)返回False
。
獲取源代碼?
- inspect.getdoc(object)?
Get the documentation string for an object, cleaned up with
cleandoc()
. If the documentation string for an object is not provided and the object is a class, a method, a property or a descriptor, retrieve the documentation string from the inheritance hierarchy. ReturnNone
if the documentation string is invalid or missing.在 3.5 版更改: 文檔字符串沒(méi)有被重寫(xiě)的話(huà)現(xiàn)在會(huì)被繼承。
- inspect.getcomments(object)?
任意多行注釋作為單一一個(gè)字符串返回。對(duì)于類(lèi)、函數(shù)和方法,選取緊貼在該對(duì)象的源代碼之前的注釋?zhuān)粚?duì)于模塊,選取 Python 源文件頂部的注釋。如果對(duì)象的源代碼不可獲得,返回
None
。這可能是因?yàn)閷?duì)象是 C 語(yǔ)言中或者是在交互式命令行中定義的。
- inspect.getfile(object)?
返回定義了這個(gè)對(duì)象的文件名(包括文本文件或二進(jìn)制文件)。如果該對(duì)象是一個(gè)內(nèi)置模塊、類(lèi)或函數(shù)則會(huì)失敗并引發(fā)一個(gè)
TypeError
。
- inspect.getmodule(object)?
Try to guess which module an object was defined in. Return
None
if the module cannot be determined.
- inspect.getsourcefile(object)?
Return the name of the Python source file in which an object was defined or
None
if no way can be identified to get the source. This will fail with aTypeError
if the object is a built-in module, class, or function.
- inspect.getsourcelines(object)?
返回一個(gè)源代碼行的列表和對(duì)象的起始行。實(shí)參可以是一個(gè)模塊、類(lèi)、方法、函數(shù)、回溯、幀或者代碼對(duì)象。與該對(duì)象相關(guān)的源代碼會(huì)按行構(gòu)成一個(gè)列表,并且會(huì)有一個(gè)行號(hào)表示其中第一行代碼出現(xiàn)在源文件的第幾行。如果源代碼不能被獲取,則會(huì)引發(fā)一個(gè)
OSError
。
- inspect.getsource(object)?
返回對(duì)象的源代碼文本。實(shí)參可以是一個(gè)模塊、類(lèi)、方法、函數(shù)、回溯、幀或者代碼對(duì)象。源代碼會(huì)作為單一一個(gè)字符串被返回。如果源代碼不能被獲取,則會(huì)引發(fā)一個(gè)
OSError
。
- inspect.cleandoc(doc)?
清理文檔字符串中為對(duì)齊當(dāng)前代碼塊進(jìn)行的縮進(jìn)
第一行的所有前綴空白符會(huì)被移除。從第二行開(kāi)始所有可以被統(tǒng)一去除的空白符也會(huì)被去除。之后,首尾的空白行也會(huì)被移除。同時(shí),所有制表符會(huì)被展開(kāi)到空格。
使用 Signature 對(duì)象對(duì)可調(diào)用對(duì)象進(jìn)行內(nèi)省?
3.3 新版功能.
Signature 對(duì)象代表了一個(gè)可調(diào)用對(duì)象的調(diào)用簽名和返回值標(biāo)注。要獲取一個(gè) Signature 對(duì)象,使用 signature()
函數(shù)。
- inspect.signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)?
返回給定的
callable
的一個(gè)Signature
對(duì)象:>>> from inspect import signature >>> def foo(a, *, b:int, **kwargs): ... pass >>> sig = signature(foo) >>> str(sig) '(a, *, b:int, **kwargs)' >>> str(sig.parameters['b']) 'b:int' >>> sig.parameters['b'].annotation <class 'int'>
接受各類(lèi)的 Python 可調(diào)用對(duì)象,包括單純的函數(shù)、類(lèi),到
functools.partial()
對(duì)象。對(duì)于在字符串化標(biāo)注模塊(
from __future__ import annotations
)中定義的對(duì)象,signature()
會(huì)嘗試使用inspect.get_annotations()
自動(dòng)地反字符串化這些標(biāo)注。參數(shù)global
locals
和eval_str
會(huì)在分析標(biāo)注時(shí)被傳入inspect.get_annotations()
。如何運(yùn)用這些參數(shù)請(qǐng)參見(jiàn)inspect.get_annotations()
的文檔。如果沒(méi)有簽名可以提供,則引發(fā)
ValueError
;如果不支持該對(duì)象類(lèi)型,則引發(fā)TypeError
。同時(shí),如果標(biāo)注被反字符串化,并且eval_str
不是假值,則eval()
會(huì)被調(diào)用以反字符串化,此時(shí)有可能引發(fā)任何種類(lèi)的異常。函數(shù)簽名中的斜杠(/)表示在它之前的參數(shù)是僅限位置的。詳見(jiàn) 編程常見(jiàn)問(wèn)題中關(guān)于僅限位置參數(shù)的條目
3.5 新版功能:
follow_wrapped
形參。傳遞False
來(lái)獲得特定關(guān)于callable
的簽名(callable.__wrapped__
將不會(huì)用來(lái)解包被修飾的可調(diào)用對(duì)象)。3.10 新版功能: 添加參數(shù)
globals
、locals
和eval_str
。備注
一些可調(diào)用對(duì)象可能在特定 Python 實(shí)現(xiàn)中無(wú)法被內(nèi)省。例如,在 CPython 中,部分通過(guò) C 語(yǔ)言定義的內(nèi)置函數(shù)不提供關(guān)于其參數(shù)的元數(shù)據(jù)。
- class inspect.Signature(parameters=None, *, return_annotation=Signature.empty)?
一個(gè) Signature 對(duì)象代表了一個(gè)函數(shù)的調(diào)用簽名和返回值標(biāo)注。對(duì)于函數(shù)接受的每個(gè)參數(shù),它對(duì)應(yīng)地在自身的
parameters
容器中存儲(chǔ)一個(gè)Parameter
對(duì)象。可選參數(shù) parameters 是一個(gè)
Parameter
對(duì)象組成的序列,它會(huì)在之后被驗(yàn)證不存在名字重復(fù)的參數(shù),并且參數(shù)處于正確的順序,即僅限位置參數(shù)最前,之后緊接著可位置可關(guān)鍵字參數(shù),并且有默認(rèn)值參數(shù)在無(wú)默認(rèn)值參數(shù)之前。可選參數(shù) return_annotation 可以是任意 Python 對(duì)象,是該可調(diào)用對(duì)象中“返回值”的標(biāo)注。
Signature 對(duì)象是 不可變 的。使用
Signature.replace()
來(lái)構(gòu)造一個(gè)修改后的副本。在 3.5 版更改: Signature 對(duì)象既可封存(pickle)又可哈希。
- empty?
該類(lèi)的一個(gè)特殊標(biāo)記來(lái)明確指出返回值標(biāo)注缺失。
- parameters?
一個(gè)參數(shù)名字到對(duì)應(yīng)
Parameter
對(duì)象的有序映射。參數(shù)以嚴(yán)格的定義順序出現(xiàn),包括僅關(guān)鍵字參數(shù)。在 3.7 版更改: Python 從 3.7 版起才顯式地保證了它保持僅關(guān)鍵字參數(shù)的定義順序,盡管實(shí)踐上在 Python 3 中一直保持了這個(gè)順序。
- return_annotation?
可調(diào)用對(duì)象的“返回值”標(biāo)注。如果可調(diào)用對(duì)象沒(méi)有“返回值”標(biāo)注,這個(gè)屬性會(huì)被設(shè)置為
Signature.empty
。
- bind(*args, **kwargs)?
構(gòu)造一個(gè)位置和關(guān)鍵字實(shí)參到形參的映射。如果
*args
和**kwargs
符合簽名,則返回一個(gè)BoundArguments
;否則引發(fā)一個(gè)TypeError
。
- bind_partial(*args, **kwargs)?
與
Signature.bind()
作用方式相同,但允許省略部分必要的參數(shù)(模仿functools.partial()
的行為)。返回BoundArguments
,或者在傳入?yún)?shù)不符合簽名的情況下,引發(fā)一個(gè)TypeError
。
- replace(*[, parameters][, return_annotation])?
基于 replace 函數(shù)被調(diào)用的目標(biāo),創(chuàng)建一個(gè)新的 Signature 實(shí)例??梢詡鬟f不同的
parameters
和/或return_annotation
來(lái)覆蓋原本簽名的對(duì)應(yīng)的屬性。要從復(fù)制的 Signature 中移除 return_annotation,可以傳入Signature.empty
。>>> def test(a, b): ... pass >>> sig = signature(test) >>> new_sig = sig.replace(return_annotation="new return anno") >>> str(new_sig) "(a, b) -> 'new return anno'"
- classmethod from_callable(obj, *, follow_wrapped=True, globalns=None, localns=None)?
返回給定可調(diào)用對(duì)象
obj
的Signature
對(duì)象(或其子類(lèi))。傳入follow_wrapped=False
來(lái)直接得到obj
的簽名,而不通過(guò)__wrapped__
鏈來(lái)解析包裝。在解析標(biāo)注時(shí)會(huì)將globalns
和localns
用作命名空間。該函數(shù)簡(jiǎn)化了創(chuàng)建
Signature
的子類(lèi)的過(guò)程:class MySignature(Signature): pass sig = MySignature.from_callable(min) assert isinstance(sig, MySignature)
3.5 新版功能.
3.10 新版功能: 添加了參數(shù)
globalns
和localns
。
- class inspect.Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)?
Parameter 對(duì)象是 不可變 的。不要直接修改 Parameter 對(duì)象,而是通過(guò)
Parameter.replace()
來(lái)創(chuàng)建一個(gè)修改后的副本。在 3.5 版更改: Parameter 對(duì)象既可封存(pickle)又可哈希。
- empty?
該類(lèi)的一個(gè)特殊標(biāo)記來(lái)明確指出默認(rèn)值和標(biāo)注的缺失。
- name?
參數(shù)的名字字符串。這個(gè)名字必須是一個(gè)合法的 Python 標(biāo)識(shí)符。
CPython implementation detail: CPython 會(huì)為用于實(shí)現(xiàn)推導(dǎo)式和生成器表達(dá)式的代碼對(duì)象構(gòu)造形如
.0
的隱式形參名。在 3.6 版更改: 這些形參名會(huì)被此模塊暴露為形如
implicit0
一樣的名字。
- default?
該參數(shù)的默認(rèn)值。如果該參數(shù)沒(méi)有默認(rèn)值,這個(gè)屬性會(huì)被設(shè)置為
Parameter.empty
。
- annotation?
該參數(shù)的標(biāo)注。如果該參數(shù)沒(méi)有標(biāo)注,這個(gè)屬性會(huì)被設(shè)置為
Parameter.empty
。
- kind?
描述實(shí)參值會(huì)如何綁定到形參??赡艿娜≈担梢酝ㄟ^(guò)
Parameter
獲得,比如Parameter.KEYWORD_ONLY
):名稱(chēng)
含意
POSITIONAL_ONLY
值必須以位置參數(shù)的方式提供。僅限位置參數(shù)是在函數(shù)定義中出現(xiàn)在
/
之前(如果有)的條目。POSITIONAL_OR_KEYWORD
值既可以以關(guān)鍵字參數(shù)的形式提供,也可以以位置參數(shù)的形式提供(這是 Python 寫(xiě)成的函數(shù)的標(biāo)準(zhǔn)綁定行為的)。
VAR_POSITIONAL
沒(méi)有綁定到其他形參的位置實(shí)參組成的元組。這對(duì)應(yīng)于 Python 函數(shù)定義中的
*args
形參。KEYWORD_ONLY
值必須以關(guān)鍵字參數(shù)的形式提供。僅限關(guān)鍵字形參是在 Python 函數(shù)定義中出現(xiàn)在
*
或*args
之后的條目。VAR_KEYWORD
一個(gè)未綁定到其他形參的關(guān)鍵字參數(shù)的字典。這對(duì)應(yīng)于 Python 函數(shù)定義中的
**kwargs
形參。例如,打印所有沒(méi)有默認(rèn)值的僅關(guān)鍵字參數(shù):
>>> def foo(a, b, *, c, d=10): ... pass >>> sig = signature(foo) >>> for param in sig.parameters.values(): ... if (param.kind == param.KEYWORD_ONLY and ... param.default is param.empty): ... print('Parameter:', param) Parameter: c
- kind.description?
描述 Parameter.kind 的枚舉值。
3.8 新版功能.
例子:打印所有參數(shù)的描述:
>>> def foo(a, b, *, c, d=10): ... pass >>> sig = signature(foo) >>> for param in sig.parameters.values(): ... print(param.kind.description) positional or keyword positional or keyword keyword-only keyword-only
- replace(*[, name][, kind][, default][, annotation])?
基于 replace 函數(shù)被調(diào)用的目標(biāo),創(chuàng)建一個(gè)新的 Parameter 實(shí)例。要覆寫(xiě)
Parameter
的屬性,傳遞對(duì)應(yīng)的參數(shù)。要移除 Parameter 的默認(rèn)值和/或標(biāo)注,傳遞Parameter.empty
。>>> from inspect import Parameter >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42) >>> str(param) 'foo=42' >>> str(param.replace()) # Will create a shallow copy of 'param' 'foo=42' >>> str(param.replace(default=Parameter.empty, annotation='spam')) "foo:'spam'"
在 3.4 版更改: 在 Python 3.3 中, Parameter 對(duì)象當(dāng)
kind
為POSITIONAL_ONLY
時(shí)允許name
被設(shè)置為None
。這現(xiàn)在已不再被允許。
- class inspect.BoundArguments?
調(diào)用
Signature.bind()
或Signature.bind_partial()
的結(jié)果。容納實(shí)參到函數(shù)的形參的映射。- arguments?
一個(gè)形參名到實(shí)參值的可變映射。僅包含顯式綁定的參數(shù)。對(duì)
arguments
的修改會(huì)反映到args
和kwargs
上。應(yīng)當(dāng)在任何參數(shù)處理目的中與
Signature.parameters
結(jié)合使用。備注
Signature.bind()
和Signature.bind_partial()
中采用默認(rèn)值的參數(shù)被跳過(guò)。然而,如果有需要的話(huà),可以使用BoundArguments.apply_defaults()
來(lái)添加它們。在 3.9 版更改:
arguments
現(xiàn)在的類(lèi)型是dict
。之前,它的類(lèi)型是collections.OrderedDict
。
- apply_defaults()?
設(shè)置缺失的參數(shù)的默認(rèn)值。
對(duì)于變長(zhǎng)位置參數(shù)(
*args
),默認(rèn)值是一個(gè)空元組。對(duì)于變長(zhǎng)關(guān)鍵字參數(shù)(
**kwargs
)默認(rèn)值是一個(gè)空字典。>>> def foo(a, b='ham', *args): pass >>> ba = inspect.signature(foo).bind('spam') >>> ba.apply_defaults() >>> ba.arguments {'a': 'spam', 'b': 'ham', 'args': ()}
3.5 新版功能.
args
和kwargs
屬性可以被用于調(diào)用函數(shù):def test(a, *, b): ... sig = signature(test) ba = sig.bind(10, b=20) test(*ba.args, **ba.kwargs)
參見(jiàn)
- PEP 362 - 函數(shù)簽名對(duì)象。
包含具體的規(guī)范,實(shí)現(xiàn)細(xì)節(jié)和樣例。
類(lèi)與函數(shù)?
- inspect.getclasstree(classes, unique=False)?
將給定的類(lèi)的列表組織成嵌套列表的層級(jí)結(jié)構(gòu)。每當(dāng)一個(gè)內(nèi)層列表出現(xiàn)時(shí),它包含的類(lèi)均派生自緊接著該列表之前的條目的類(lèi)。每個(gè)條目均是一個(gè)二元組,包含一個(gè)類(lèi)和它的基類(lèi)組成的元組。如果 unique 參數(shù)為真值,則給定列表中的每個(gè)類(lèi)將恰有一個(gè)對(duì)應(yīng)條目。否則,運(yùn)用了多重繼承的類(lèi)和它們的后代將出現(xiàn)多次。
- inspect.getfullargspec(func)?
獲取一個(gè) Python 函數(shù)的形參的名字和默認(rèn)值。將返回一個(gè) 具名元組:
FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)
args 是一個(gè)位置參數(shù)的名字的列表。 varargs 是
*
形參的名字或None
表示不接受任意長(zhǎng)位置參數(shù)時(shí)。 varkw 是**
參數(shù)的名字,或None
表示不接受任意關(guān)鍵字參數(shù)。 defaults 是一個(gè)包含了默認(rèn)參數(shù)值的 n 元組分別對(duì)應(yīng)最后 n 個(gè)位置參數(shù),或None
則表示沒(méi)有默認(rèn)值。 kwonlyargs 是一個(gè)僅關(guān)鍵詞參數(shù)列表,保持定義時(shí)的順序。 kwonlydefaults 是一個(gè)字典映射自 kwonlyargs 中包含的形參名。 annotations 是一個(gè)字典,包含形參值到標(biāo)注的映射。其中包含一個(gè)特殊的鍵"return"
代表函數(shù)返回值的標(biāo)注(如果有的話(huà))。注意:
signature()
和 Signature 對(duì)象 提供可調(diào)用對(duì)象內(nèi)省更推薦的 API,并且支持?jǐn)U展模塊 API 中可能出現(xiàn)的額外的行為(比如僅限位置參數(shù))。該函數(shù)被保留的主要原因是保持兼容 Python 2 的inspect
模塊 API。在 3.4 版更改: 該函數(shù)現(xiàn)在基于
signature()
但仍然忽略__wrapped__
屬性,并且在簽名中包含綁定方法中的第一個(gè)綁定參數(shù)。在 3.6 版更改: 該方法在 Python 3.5 中曾因
signature()
被文檔歸為棄用。但該決定已被推翻以恢復(fù)一個(gè)明確受支持的標(biāo)準(zhǔn)接口,以便運(yùn)用一份源碼通用 Python 2/3 間遺留的getargspec()
API 的遷移。在 3.7 版更改: Python 從 3.7 版起才顯式地保證了它保持僅關(guān)鍵字參數(shù)的定義順序,盡管實(shí)踐上在 Python 3 中一直保持了這個(gè)順序。
- inspect.getargvalues(frame)?
獲取傳入特定的幀的實(shí)參的信息。將返回一個(gè) 具名元組
ArgInfo(args, varargs, keywords, locals)
。 args 是一個(gè)參數(shù)名字的列表。 varargs 和 keyword 是*
和**
參數(shù)的名字或None
。 locals 是給定的幀的局部環(huán)境字典。備注
該函數(shù)因疏忽在 Python 3.5 中被錯(cuò)誤地標(biāo)記為棄用。
- inspect.formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])?
將
getargvalues()
返回的四個(gè)值格式化為美觀的參數(shù)規(guī)格。 format* 的參數(shù)是對(duì)應(yīng)的可選格式化函數(shù)以轉(zhuǎn)化名字和值為字符串。備注
該函數(shù)因疏忽在 Python 3.5 中被錯(cuò)誤地標(biāo)記為棄用。
- inspect.getmro(cls)?
返回由類(lèi) cls 的全部基類(lèi)按方法解析順序組成的元組,包括 cls 本身。所有類(lèi)不會(huì)在此元組中出現(xiàn)多于一次。注意方法解析順序取決于 cls 的類(lèi)型。除非使用一個(gè)非常奇怪的用戶(hù)定義元類(lèi)型,否則 cls 會(huì)是元組的第一個(gè)元素。
- inspect.getcallargs(func, /, *args, **kwds)?
仿照調(diào)用方式綁定 args 和 kwds 到 Python 函數(shù)或方法 func 參數(shù)名。對(duì)于綁定方法,也綁定第一個(gè)參數(shù)(通常命名為
self
)到關(guān)聯(lián)的實(shí)例。返回一個(gè)字典,映射自參數(shù)名(包括可能存在的*
和**
參數(shù))到他們對(duì)應(yīng)于 args 和 kwds 中的值。假使 func 被錯(cuò)誤地調(diào)用,即是說(shuō)func(*args, **kwds)
會(huì)因函數(shù)簽名不一致引發(fā)一個(gè)異常,那么也會(huì)引發(fā)一個(gè)相同種類(lèi)的異常,并附上相同或類(lèi)似的消息。例如:>>> from inspect import getcallargs >>> def f(a, b=1, *pos, **named): ... pass >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} True >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} True >>> getcallargs(f) Traceback (most recent call last): ... TypeError: f() missing 1 required positional argument: 'a'
3.2 新版功能.
3.5 版后已移除: 改使用
Signature.bind()
和Signature.bind_partial()
。
- inspect.getclosurevars(func)?
獲取自 Python 函數(shù)或方法 func 引用的外部名字到它們的值的映射。返回一個(gè) 具名元組
ClosureVars(nonlocals, globals, builtins, unbound)
。 nonlocals 映射引用的名字到詞法閉包變量,globals 映射到函數(shù)的模塊級(jí)全局, builtins 映射到函數(shù)體內(nèi)可見(jiàn)的內(nèi)置變量。 unbound 是在函數(shù)中引用但不能解析到給定的模塊全局和內(nèi)置變量的名字的集合。如果 func 不是 Python 函數(shù)或方法,將引發(fā)
TypeError
。3.3 新版功能.
- inspect.unwrap(func, *, stop=None)?
獲取 func 所包裝的對(duì)象。它追蹤
__wrapped__
屬性鏈并返回最后一個(gè)對(duì)象。stop 是一個(gè)可選的回調(diào),接受包裝鏈的一個(gè)對(duì)象作為唯一參數(shù),以允許通過(guò)讓回調(diào)返回真值使解包裝更早中止。如果回調(diào)不曾返回一個(gè)真值,將如常返回鏈中的最后一個(gè)對(duì)象。例如,
signature()
使用該參數(shù)來(lái)在遇到具有__signature__
參數(shù)的對(duì)象時(shí)停止解包裝。如果遇到循環(huán),則引發(fā)
ValueError
。3.4 新版功能.
- inspect.get_annotations(obj, *, globals=None, locals=None, eval_str=False)?
計(jì)算一個(gè)對(duì)象的標(biāo)注字典。
obj
可以是一個(gè)可調(diào)用對(duì)象、類(lèi)或模塊。傳入其他類(lèi)型的對(duì)象將引發(fā)TypeError
。返回一個(gè)字典。
get_annotations()
每次調(diào)用均返回一個(gè)新的字典;對(duì)同一個(gè)對(duì)象調(diào)用兩次將獲得兩個(gè)不同但相等的字典。該函數(shù)幫助你處理若干細(xì)節(jié):
如果
eval_str
為真,str
類(lèi)型的值將會(huì)通過(guò)eval()
來(lái)反字符串化。這被設(shè)計(jì)用于字符串化標(biāo)注(from __future__ import annotations
)。如果
obj
不包含一個(gè)標(biāo)注字典,返回一個(gè)空字典。(函數(shù)和方法永遠(yuǎn)包含一個(gè)標(biāo)注字典;類(lèi)、模塊和其他類(lèi)型的可調(diào)用對(duì)象則可能沒(méi)有。)對(duì)于類(lèi),忽略繼承而來(lái)的標(biāo)注。如果一個(gè)類(lèi)不包含自己的標(biāo)注字典,返回一個(gè)空字典。
因安全原因,所有對(duì)于對(duì)象成員和字典值的訪問(wèn)將通過(guò)
getattr()
和dict.get()
完成。永遠(yuǎn),永遠(yuǎn),永遠(yuǎn)返回一個(gè)新建立的字典。
eval_str
控制str
類(lèi)型的值是否應(yīng)該替換為對(duì)其調(diào)用eval()
的結(jié)果:如果 eval_str 為真,
eval()
會(huì)被調(diào)用于str
類(lèi)型。(注意get_annotations
并不捕獲異常;如果eval()
返回一個(gè)錯(cuò)誤,它會(huì)將棧展開(kāi)跳過(guò)get_annotations
調(diào)用。)如果 eval_str 為假(默認(rèn)值),
str
類(lèi)型的值將不會(huì)被改變。
globals
和locals
會(huì)被直接傳入函數(shù)eval()
,詳見(jiàn)eval()
的文檔。如果globals
或者locals
是None
,則改函數(shù)視type(obj)
而定,可能將相應(yīng)的值替換為一個(gè)上下文有關(guān)的默認(rèn)值。如果
obj
是一個(gè)模塊,globals
默認(rèn)為obj.__dict__
。如果
obj
是一個(gè)類(lèi),globals
默認(rèn)值為sys.modules[obj.__module__].__dict__
并且locals
默認(rèn)值為obj
的類(lèi)命名空間。如果
obj
是一個(gè)可調(diào)用對(duì)象,globals
默認(rèn)為obj.__globals__
,而如果obj
是一個(gè)包裝過(guò)的函數(shù),則它會(huì)先被解包裝。
調(diào)用
get_annotations
是獲取任何對(duì)象的標(biāo)注字典的最佳實(shí)踐。關(guān)于標(biāo)注的最佳實(shí)踐的更多信息,參見(jiàn) 對(duì)象注解屬性的最佳實(shí)踐。3.10 新版功能.
解釋器棧?
Some of the following functions return
FrameInfo
objects. For backwards compatibility these objects allow
tuple-like operations on all attributes except positions
. This behavior
is considered deprecated and may be removed in the future.
- class inspect.FrameInfo?
- frame?
The frame object that the record corresponds to.
- filename?
The file name associated with the code being executed by the frame this record corresponds to.
- lineno?
The line number of the current line associated with the code being executed by the frame this record corresponds to.
- function?
The function name that is being executed by the frame this record corresponds to.
- code_context?
A list of lines of context from the source code that's being executed by the frame this record corresponds to.
- index?
The index of the current line being executed in the
code_context
list.
- positions?
A
dis.Positions
object containing the start line number, end line number, start column offset, and end column offset associated with the instruction being executed by the frame this record corresponds to.
在 3.5 版更改: 不再返回一個(gè)元組而是返回一個(gè)具名元組。
在 3.11 版更改: Changed the return object from a named tuple to a regular object (that is backwards compatible with the previous named tuple).
- class inspect.Traceback?
- filename?
The file name associated with the code being executed by the frame this traceback corresponds to.
- lineno?
The line number of the current line associated with the code being executed by the frame this traceback corresponds to.
- function?
The function name that is being executed by the frame this traceback corresponds to.
- code_context?
A list of lines of context from the source code that's being executed by the frame this traceback corresponds to.
- index?
The index of the current line being executed in the
code_context
list.
- positions?
A
dis.Positions
object containing the start line number, end line number, start column offset, and end column offset associated with the instruction being executed by the frame this traceback corresponds to.
備注
保留幀對(duì)象的引用(可見(jiàn)于這些函數(shù)返回的幀記錄的第一個(gè)元素)會(huì)導(dǎo)致你的程序產(chǎn)生循環(huán)引用。每當(dāng)一個(gè)循環(huán)引用被創(chuàng)建,所有可從產(chǎn)生循環(huán)的對(duì)象訪問(wèn)的對(duì)象的生命周期將會(huì)被大幅度延長(zhǎng),即便 Python 的可選的循環(huán)檢測(cè)器被啟用。如果這類(lèi)循環(huán)必須被創(chuàng)建,確保它們會(huì)被顯式地打破以避免對(duì)象銷(xiāo)毀被延遲從而導(dǎo)致占用內(nèi)存增加。
盡管循環(huán)檢測(cè)器能夠處理這種情況,這些幀(包括其局部變量)的銷(xiāo)毀可以通過(guò)在 finally
子句中移除循環(huán)來(lái)產(chǎn)生確定的行為。對(duì)于循環(huán)檢測(cè)器在編譯 Python 時(shí)被禁用或者使用 gc.disable()
時(shí),這樣處理更加尤為重要。比如:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
# do something with the frame
finally:
del frame
如果你希望保持幀更長(zhǎng)的時(shí)間(比如在之后打印回溯),你也可以通過(guò) frame.clear()
方法打破循環(huán)引用。
大部分這些函數(shù)支持的可選的 context 參數(shù)指定返回時(shí)包含的上下文的行數(shù),以當(dāng)前行為中心。
- inspect.getframeinfo(frame, context=1)?
Get information about a frame or traceback object. A
Traceback
object is returned.在 3.11 版更改: A
Traceback
object is returned instead of a named tuple.
- inspect.getouterframes(frame, context=1)?
Get a list of
FrameInfo
objects for a frame and all outer frames. These frames represent the calls that lead to the creation of frame. The first entry in the returned list represents frame; the last entry represents the outermost call on frame's stack.在 3.5 版更改: 返回一個(gè) 具名元組
FrameInfo(frame, filename, lineno, function, code_context, index)
的列表。在 3.11 版更改: A list of
FrameInfo
objects is returned.
- inspect.getinnerframes(traceback, context=1)?
Get a list of
FrameInfo
objects for a traceback's frame and all inner frames. These frames represent calls made as a consequence of frame. The first entry in the list represents traceback; the last entry represents where the exception was raised.在 3.5 版更改: 返回一個(gè) 具名元組
FrameInfo(frame, filename, lineno, function, code_context, index)
的列表。在 3.11 版更改: A list of
FrameInfo
objects is returned.
- inspect.currentframe()?
返回調(diào)用者的棧幀對(duì)應(yīng)的幀對(duì)象。
CPython implementation detail: 該函數(shù)依賴(lài)于 Python 解釋器對(duì)于棧幀的支持,這并非在 Python 的所有實(shí)現(xiàn)中被保證。該函數(shù)在不支持Python 棧幀的實(shí)現(xiàn)中運(yùn)行會(huì)返回
None
。
- inspect.stack(context=1)?
Return a list of
FrameInfo
objects for the caller's stack. The first entry in the returned list represents the caller; the last entry represents the outermost call on the stack.在 3.5 版更改: 返回一個(gè) 具名元組
FrameInfo(frame, filename, lineno, function, code_context, index)
的列表。在 3.11 版更改: A list of
FrameInfo
objects is returned.
- inspect.trace(context=1)?
Return a list of
FrameInfo
objects for the stack between the current frame and the frame in which an exception currently being handled was raised in. The first entry in the list represents the caller; the last entry represents where the exception was raised.在 3.5 版更改: 返回一個(gè) 具名元組
FrameInfo(frame, filename, lineno, function, code_context, index)
的列表。在 3.11 版更改: A list of
FrameInfo
objects is returned.
靜態(tài)地獲取屬性?
getattr()
和 hasattr()
都可能會(huì)在獲取或者判斷屬性是否存在時(shí)觸發(fā)代碼執(zhí)行。描述符,就和特性一樣,會(huì)被調(diào)用, __getattr__()
和 __getattribute__()
可能會(huì)被調(diào)用。
對(duì)于你想要靜態(tài)地內(nèi)省的情況,比如文檔工具,這會(huì)顯得不方便。 getattr_static()
擁有與 getattr()
相同的簽名,但避免了獲取屬性時(shí)執(zhí)行代碼。
- inspect.getattr_static(obj, attr, default=None)?
獲取屬性而不觸發(fā)描述器協(xié)議的動(dòng)態(tài)查找能力
__getattr__()
或__getattribute__()
。注意:該函數(shù)可能無(wú)法獲取 getattr 能獲取的全部的屬性(比如動(dòng)態(tài)地創(chuàng)建的屬性),并且可能發(fā)現(xiàn)一些 getattr 無(wú)法找到的屬性(比如描述器會(huì)引發(fā) AttributeError)。它也能夠返回描述器對(duì)象本身而非實(shí)例成員。
如果實(shí)例的
__dict__
被其他成員遮蓋(比如一個(gè)特性)則該函數(shù)無(wú)法找到實(shí)例成員。3.2 新版功能.
getattr_static()
不解析描述器。比如槽描述器或 C 語(yǔ)言中實(shí)現(xiàn)的 getset 描述器。該描述器對(duì)象會(huì)被直接返回,而不處理底層屬性。
你可以用類(lèi)似下方的代碼的方法處理此事。注意,對(duì)于任意 getset 描述符,使用這段代碼仍可能觸發(fā)代碼執(zhí)行。
# example code for resolving the builtin descriptor types
class _foo:
__slots__ = ['foo']
slot_descriptor = type(_foo.foo)
getset_descriptor = type(type(open(__file__)).name)
wrapper_descriptor = type(str.__dict__['__add__'])
descriptor_types = (slot_descriptor, getset_descriptor, wrapper_descriptor)
result = getattr_static(some_object, 'foo')
if type(result) in descriptor_types:
try:
result = result.__get__()
except AttributeError:
# descriptors can raise AttributeError to
# indicate there is no underlying value
# in which case the descriptor itself will
# have to do
pass
生成器和協(xié)程的當(dāng)前狀態(tài)?
當(dāng)實(shí)現(xiàn)協(xié)程調(diào)度器或其他更高級(jí)的生成器用途時(shí),判斷一個(gè)生成器是正在執(zhí)行、等待啟動(dòng)或繼續(xù)或執(zhí)行,又或者已經(jīng)被終止是非常有用的。 getgeneratorstate()
允許方便地判斷一個(gè)生成器的當(dāng)前狀態(tài)。
- inspect.getgeneratorstate(generator)?
獲取生成器迭代器的當(dāng)前狀態(tài)。
- 可能的狀態(tài)是:
GEN_CREATED:等待開(kāi)始執(zhí)行。
GEN_RUNNING:正在被解釋器執(zhí)行。
GEN_SUSPENDED:當(dāng)前掛起于一個(gè) yield 表達(dá)式。
GEN_CLOSED:執(zhí)行已經(jīng)完成。
3.2 新版功能.
- inspect.getcoroutinestate(coroutine)?
獲取協(xié)程對(duì)象的當(dāng)前狀態(tài)。該函數(shù)設(shè)計(jì)為用于使用
async def
函數(shù)創(chuàng)建的協(xié)程函數(shù),但也能接受任何包括cr_running
和cr_frame
的類(lèi)似協(xié)程的對(duì)象。- 可能的狀態(tài)是:
CORO_CREATED:等待開(kāi)始執(zhí)行。
CORO_RUNNING:當(dāng)前正在被解釋器執(zhí)行。
CORO_SUSPENDED:當(dāng)前掛起于一個(gè) await 表達(dá)式。
CORO_CLOSED:執(zhí)行已經(jīng)完成。
3.5 新版功能.
生成器當(dāng)前的內(nèi)部狀態(tài)也可以被查詢(xún)。這通常在測(cè)試目的中最為有用,來(lái)保證內(nèi)部狀態(tài)如預(yù)期一樣被更新:
- inspect.getgeneratorlocals(generator)?
獲取 generator 里的實(shí)時(shí)局部變量到當(dāng)前值的映射。返回一個(gè)由名字映射到值的字典。這與在生成器的主體內(nèi)調(diào)用
locals()
是等效的,并且相同的警告也適用。如果 generator 是一個(gè)沒(méi)有關(guān)聯(lián)幀的 生成器,則返回一個(gè)空字典。如果 generator 不是一個(gè) Python 生成器對(duì)象,則引發(fā)
TypeError
。CPython implementation detail: 該函數(shù)依賴(lài)于生成器為內(nèi)省暴露一個(gè) Python 棧幀,這并非在 Python 的所有實(shí)現(xiàn)中被保證。在這種情況下,該函數(shù)將永遠(yuǎn)返回一個(gè)空字典。
3.3 新版功能.
- inspect.getcoroutinelocals(coroutine)?
該函數(shù)可類(lèi)比于
getgeneratorlocals()
,只是作用于由async def
函數(shù)創(chuàng)建的協(xié)程。3.5 新版功能.
代碼對(duì)象位標(biāo)志?
Python 代碼對(duì)象有一個(gè) co_flags
屬性,它是下列標(biāo)志的位圖。
- inspect.CO_OPTIMIZED?
代碼對(duì)象已經(jīng)經(jīng)過(guò)優(yōu)化,會(huì)采用快速局部變量。
- inspect.CO_NEWLOCALS?
如果被置位,當(dāng)代碼對(duì)象被執(zhí)行時(shí)會(huì)創(chuàng)建一個(gè)新的字典作為幀的
f_locals
。
- inspect.CO_VARARGS?
代碼對(duì)象擁有一個(gè)變長(zhǎng)位置形參(類(lèi)似
*args
)。
- inspect.CO_VARKEYWORDS?
代碼對(duì)象擁有一個(gè)可變關(guān)鍵字形慘死(類(lèi)似
**kwrags
)。
- inspect.CO_NESTED?
該標(biāo)志當(dāng)代碼對(duì)象是一個(gè)嵌套函數(shù)時(shí)被置位。
- inspect.CO_GENERATOR?
當(dāng)代碼對(duì)象是一個(gè)生成器函數(shù),即調(diào)用時(shí)會(huì)返回一個(gè)生成器對(duì)象,則該標(biāo)志被置位。
- inspect.CO_COROUTINE?
當(dāng)代碼對(duì)象是一個(gè)協(xié)程函數(shù)時(shí)被置位。當(dāng)代碼對(duì)象被執(zhí)行時(shí)它返回一個(gè)協(xié)程。詳見(jiàn) PEP 492。
3.5 新版功能.
- inspect.CO_ITERABLE_COROUTINE?
該標(biāo)志被用于將生成器轉(zhuǎn)變?yōu)榛谏善鞯膮f(xié)程。包含此標(biāo)志的生成器對(duì)象可以被用于
await
表達(dá)式,并可以yield from
協(xié)程對(duì)象。詳見(jiàn) PEP 492。3.5 新版功能.
- inspect.CO_ASYNC_GENERATOR?
當(dāng)代碼對(duì)象是一個(gè)異步生成器函數(shù)時(shí)該標(biāo)志被置位。當(dāng)代碼對(duì)象被運(yùn)行時(shí)它將返回一個(gè)異步生成器對(duì)象。詳見(jiàn) PEP 525。
3.6 新版功能.
備注
這些標(biāo)志特指于 CPython,并且在其他 Python 實(shí)現(xiàn)中可能從未被定義。更進(jìn)一步地說(shuō),這些標(biāo)志是一種實(shí)現(xiàn)細(xì)節(jié),并且可能在將來(lái)的 Python 發(fā)行中被移除或棄用。推薦使用 inspect
模塊的公共 API 來(lái)進(jìn)行任何內(nèi)省需求。
命令行界面?
inspect
模塊也提供一個(gè)從命令行使用基本的內(nèi)省能力。
默認(rèn)地,命令行接受一個(gè)模塊的名字并打印模塊的源代碼。也可通過(guò)后綴一個(gè)冒號(hào)和目標(biāo)對(duì)象的限定名稱(chēng)來(lái)打印一個(gè)類(lèi)或者一個(gè)函數(shù)。
- --details?
打印特定對(duì)象的信息而非源碼。