inspect --- 檢查對象?

源代碼: Lib/inspect.py


inspect 模塊提供了一些有用的函數(shù)幫助獲取對象的信息,例如模塊、類、方法、函數(shù)、回溯、幀對象以及代碼對象。例如它可以幫助你檢查類的內(nèi)容,獲取某個方法的源代碼,取得并格式化某個函數(shù)的參數(shù)列表,或者獲取你需要顯示的回溯的詳細(xì)信息。

該模塊提供了4種主要的功能:類型檢查、獲取源代碼、檢查類與函數(shù)、檢查解釋器的調(diào)用堆棧。

類型和成員?

getmembers() 函數(shù)獲取對象的成員,例如類或模塊。函數(shù)名以"is"開始的函數(shù)主要作為 getmembers() 的第2個參數(shù)使用。它們也可用于判定某對象是否有如下的特殊屬性:

類型

屬性

描述

module -- 模塊

__doc__

文檔字符串

__file__

文件名(內(nèi)置模塊沒有文件名)

class -- 類

__doc__

文檔字符串

__name__

類定義時所使用的名稱

__qualname__

qualified name -- 限定名稱

__module__

該類型被定義時所在的模塊的名稱

method -- 方法

__doc__

文檔字符串

__name__

該方法定義時所使用的名稱

__qualname__

qualified name -- 限定名稱

__func__

實現(xiàn)該方法的函數(shù)對象

__self__

該方法被綁定的實例,若沒有綁定則為 None

__module__

定義此方法的模塊的名稱

function -- 函數(shù)

__doc__

文檔字符串

__name__

用于定義此函數(shù)的名稱

__qualname__

qualified name -- 限定名稱

__code__

包含已編譯函數(shù)的代碼對象 bytecode

__defaults__

所有位置或關(guān)鍵字參數(shù)的默認(rèn)值的元組

__kwdefaults__

保存僅關(guān)鍵字參數(shù)的所有默認(rèn)值的映射

__globals__

此函數(shù)定義所在的全局命名空間

__builtins__

builtins 命名空間

__annotations__

參數(shù)名稱到注解的映射;保留鍵 "return" 用于返回值注解。

__module__

此函數(shù)定義所在的模塊名稱

traceback -- 回溯

tb_frame

此層的幀對象

tb_lasti

在字節(jié)碼中最后嘗試的指令的索引

tb_lineno

當(dāng)前行在 Python 源代碼中的行號

tb_next

下一個內(nèi)部回溯對象(由本層調(diào)用)

frame -- 幀

f_back

下一個外部幀對象(此幀的調(diào)用者)

f_builtins

此幀執(zhí)行時所在的 builtins 命名空間

f_code

在此幀中執(zhí)行的代碼對象

f_globals

此幀執(zhí)行時所在的全局命名空間

f_lasti

在字節(jié)碼中最后嘗試的指令的索引

f_lineno

當(dāng)前行在 Python 源代碼中的行號

f_locals

此幀所看到的局部命名空間

f_trace

此幀的追蹤函數(shù),或``None``

code -- 代碼

co_argcount

參數(shù)數(shù)量(不包括僅關(guān)鍵字參數(shù)、* 或 ** 參數(shù))

co_code

字符串形式的原始字節(jié)碼

co_cellvars

單元變量名稱的元組(通過包含作用域引用)

co_consts

字節(jié)碼中使用的常量元組

co_filename

創(chuàng)建此代碼對象的文件的名稱

co_firstlineno

第一行在Python源代碼中的行號

co_flags

CO_* 標(biāo)志的位圖,詳見 此處

co_lnotab

編碼的行號到字節(jié)碼索引的映射

co_freevars

自由變量的名字組成的元組(通過函數(shù)閉包引用)

co_posonlyargcount

僅限位置參數(shù)的數(shù)量

co_kwonlyargcount

僅限關(guān)鍵字參數(shù)的數(shù)量(不包括 ** 參數(shù))

co_name

定義此代碼對象的名稱

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__

名稱

__qualname__

qualified name -- 限定名稱

gi_frame

frame -- 幀

gi_running

生成器在運(yùn)行嗎?

gi_code

code -- 代碼

gi_yieldfrom

通過``yield from``迭代的對象,或``None``

coroutine -- 協(xié)程

__name__

名稱

__qualname__

qualified name -- 限定名稱

cr_await

正在等待的對象,或``None``

cr_frame

frame -- 幀

cr_running

這個協(xié)程正在運(yùn)行嗎?

cr_code

code -- 代碼

cr_origin

協(xié)程被創(chuàng)建的位置,或``None``。參見 sys.set_coroutine_origin_tracking_depth()

builtin

__doc__

文檔字符串

__name__

此函數(shù)或方法的原始名稱

__qualname__

qualified name -- 限定名稱

__self__

方法綁定到的實例,或``None``

在 3.5 版更改: 為生成器添加``__qualname__``和``gi_yieldfrom``屬性。

生成器的``__name__``屬性現(xiàn)在由函數(shù)名稱設(shè)置,而不是代碼對象名稱,并且現(xiàn)在可以被修改。

在 3.7 版更改: 為協(xié)程添加``cr_origin``屬性。

在 3.10 版更改: 為函數(shù)添加``__builtins__``屬性。

inspect.getmembers(object[, predicate])?

返回一個對象上的所有成員,組成以 (名稱, 值) 對為元素的列表,按名稱排序。如果提供了可選的 predicate 參數(shù)(會對每個成員的 對象進(jìn)行一次調(diào)用),則僅包含該斷言為真的成員。

備注

如果要讓 getmembers() 返回元類中定義的類屬性,那么實參須為一個類且這些屬性在元類的自定義方法 __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ò)展名會檢查是否在 importlib.machinery.all_suffixes() 列出的條目中。如果符合,則文件路徑的最后一個組成部分會去掉后綴名后被返回;否則返回``None``。

值得注意的是,這個函數(shù) 返回可以作為 Python 模塊的名字,而有可能指向一個 Python 包的路徑仍然會返回``None``。

在 3.3 版更改: 該函數(shù)直接基于 importlib。

inspect.ismodule(object)?

當(dāng)該對象是一個模塊時返回``True``。

inspect.isclass(object)?

當(dāng)該對象是一個類時返回``True``,無論是內(nèi)置類或者 Python 代碼中定義的類。

inspect.ismethod(object)?

當(dāng)該對象是一個 Python 寫成的綁定方法時返回``True``。

inspect.isfunction(object)?

當(dāng)該對象是一個 Python 函數(shù)時(包括使用 lambda 表達(dá)式創(chuàng)造的函數(shù)),返回``True``。

inspect.isgeneratorfunction(object)?

當(dāng)該對象是一個 Python 生成器函數(shù)時返回``True``。

在 3.8 版更改: 對于使用 functools.partial() 封裝的函數(shù),如果被封裝的函數(shù)是一個 Python 生成器函數(shù),現(xiàn)在也會返回``True``。

inspect.isgenerator(object)?

當(dāng)該對象是一個生成器時返回``True``。

inspect.iscoroutinefunction(object)?

當(dāng)該對象是一個 協(xié)程函數(shù) (通過 async def 語法定義的函數(shù))時返回``True``。

3.5 新版功能.

在 3.8 版更改: 對于使用 functools.partial() 封裝的函數(shù),如果被封裝的函數(shù)是一個 協(xié)程函數(shù) ,現(xiàn)在也會返回``True``。

inspect.iscoroutine(object)?

當(dāng)該對象是一個由 async def 函數(shù)創(chuàng)建的 協(xié)程 時返回``True``。

3.5 新版功能.

inspect.isawaitable(object)?

如果該對象可以在 await 表達(dá)式中使用時返回``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)?

如果該對象是一個 異步生成器 函數(shù)則返回``True``,例如:

>>>
>>> async def agen():
...     yield 1
...
>>> inspect.isasyncgenfunction(agen)
True

3.6 新版功能.

在 3.8 版更改: 對于使用 functools.partial() 封裝的函數(shù),如果被封裝的函數(shù)是一個 異步生成器 ,現(xiàn)在也會返回``True``。

inspect.isasyncgen(object)?

如果該對象是一個由 異步生成器 函數(shù)創(chuàng)建的 異步生成器迭代器,則返回 True。

3.6 新版功能.

inspect.istraceback(object)?

如果該對象是一個回溯則返回 True。

inspect.isframe(object)?

如果該對象是一個幀對象則返回 True。

inspect.iscode(object)?

如果該對象是一個代碼對象則返回 True。

inspect.isbuiltin(object)?

如果該對象是一個內(nèi)置函數(shù)或一個綁定的內(nèi)置方法,則返回 True

inspect.ismethodwrapper(object)?

Return True if the type of object is a MethodWrapperType.

These are instances of MethodWrapperType, such as __str__(), __eq__() and __repr__()

inspect.isroutine(object)?

如果該對象是一個用戶定義的或內(nèi)置的函數(shù)或者方法,則返回 True

inspect.isabstract(object)?

如果該對象是一個抽象基類則返回 True。

inspect.ismethoddescriptor(object)?

如果該對象是一個方法描述器,但 ismethod() 、 isclass() 、 isfunction()isbuiltin() 均不為真,則返回 True。

例如,該函數(shù)對于 int.__add__ 為真。一個通過此測試的對象可以有 __get__() 方法,但不能有 __set__() 方法,除此以外的屬性集合是可變的。一個 __name__ 屬性通常是合理的,而 __doc__ 屬性常常也是。

即使是通過描述器實現(xiàn)的函數(shù),如果通過其他某一個測試,則 ismethoddescriptor() 測試則會返回 False。這單純是因為其他測試提供了更多保證。比如,當(dāng)一個對象通過 ismethod() 測試時,你可以使用 __func__ 等屬性。

inspect.isdatadescriptor(object)?

如果該對象是一個數(shù)據(jù)描述器則返回 True。

數(shù)據(jù)描述器擁有 __set____delete__ 方法。比如屬性(通過 Python 定義)、getset 和成員。后二者是通過 C 定義的,并且也有對應(yīng)的更具體的測試,并且在不同的 Python 實現(xiàn)中也是健壯的。典型地,數(shù)據(jù)描述器會擁有 __name____doc__ 屬性(屬性、getset 和成員都包含這兩個屬性),但這并無保證。

inspect.isgetsetdescriptor(object)?

如果該對象是一個 getset 描述器則返回 True。

CPython implementation detail: getset 是在擴(kuò)展模塊中通過 PyGetSetDef 結(jié)構(gòu)體定義的屬性。對于不包含這種類型的 Python 實現(xiàn),這個方法將永遠(yuǎn)返回 False。

inspect.ismemberdescriptor(object)?

如果該對象是一個成員描述器則返回 True。

CPython implementation detail: 成員描述器是在擴(kuò)展模塊中通過 PyMemberDef 結(jié)構(gòu)體定義的屬性。對于不包含這種類型的 Python 實現(xiàn),這個方法將永遠(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. Return None if the documentation string is invalid or missing.

在 3.5 版更改: 文檔字符串沒有被重寫的話現(xiàn)在會被繼承。

inspect.getcomments(object)?

任意多行注釋作為單一一個字符串返回。對于類、函數(shù)和方法,選取緊貼在該對象的源代碼之前的注釋;對于模塊,選取 Python 源文件頂部的注釋。如果對象的源代碼不可獲得,返回 None。這可能是因為對象是 C 語言中或者是在交互式命令行中定義的。

inspect.getfile(object)?

返回定義了這個對象的文件名(包括文本文件或二進(jìn)制文件)。如果該對象是一個內(nèi)置模塊、類或函數(shù)則會失敗并引發(fā)一個 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 a TypeError if the object is a built-in module, class, or function.

inspect.getsourcelines(object)?

返回一個源代碼行的列表和對象的起始行。實參可以是一個模塊、類、方法、函數(shù)、回溯、幀或者代碼對象。與該對象相關(guān)的源代碼會按行構(gòu)成一個列表,并且會有一個行號表示其中第一行代碼出現(xiàn)在源文件的第幾行。如果源代碼不能被獲取,則會引發(fā)一個 OSError。

在 3.3 版更改: 現(xiàn)在會引發(fā) OSError 而不是 IOError,后者現(xiàn)在是前者的一個別名。

inspect.getsource(object)?

返回對象的源代碼文本。實參可以是一個模塊、類、方法、函數(shù)、回溯、幀或者代碼對象。源代碼會作為單一一個字符串被返回。如果源代碼不能被獲取,則會引發(fā)一個 OSError。

在 3.3 版更改: 現(xiàn)在會引發(fā) OSError 而不是 IOError,后者現(xiàn)在是前者的一個別名。

inspect.cleandoc(doc)?

清理文檔字符串中為對齊當(dāng)前代碼塊進(jìn)行的縮進(jìn)

第一行的所有前綴空白符會被移除。從第二行開始所有可以被統(tǒng)一去除的空白符也會被去除。之后,首尾的空白行也會被移除。同時,所有制表符會被展開到空格。

使用 Signature 對象對可調(diào)用對象進(jìn)行內(nèi)省?

3.3 新版功能.

Signature 對象代表了一個可調(diào)用對象的調(diào)用簽名和返回值標(biāo)注。要獲取一個 Signature 對象,使用 signature() 函數(shù)。

inspect.signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)?

返回給定的 callable 的一個 Signature 對象:

>>>
>>> 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'>

接受各類的 Python 可調(diào)用對象,包括單純的函數(shù)、類,到 functools.partial() 對象。

對于在字符串化標(biāo)注模塊(from __future__ import annotations)中定義的對象, signature() 會嘗試使用 inspect.get_annotations() 自動地反字符串化這些標(biāo)注。參數(shù) global localseval_str 會在分析標(biāo)注時被傳入 inspect.get_annotations() 。如何運(yùn)用這些參數(shù)請參見 inspect.get_annotations() 的文檔。

如果沒有簽名可以提供,則引發(fā) ValueError;如果不支持該對象類型,則引發(fā) TypeError。同時,如果標(biāo)注被反字符串化,并且 eval_str 不是假值,則 eval() 會被調(diào)用以反字符串化,此時有可能引發(fā)任何種類的異常。

函數(shù)簽名中的斜杠(/)表示在它之前的參數(shù)是僅限位置的。詳見 編程常見問題中關(guān)于僅限位置參數(shù)的條目

3.5 新版功能: follow_wrapped 形參。傳遞 False 來獲得特定關(guān)于 callable 的簽名(callable.__wrapped__ 將不會用來解包被修飾的可調(diào)用對象)。

3.10 新版功能: 添加參數(shù) globals 、 localseval_str。

備注

一些可調(diào)用對象可能在特定 Python 實現(xiàn)中無法被內(nèi)省。例如,在 CPython 中,部分通過 C 語言定義的內(nèi)置函數(shù)不提供關(guān)于其參數(shù)的元數(shù)據(jù)。

class inspect.Signature(parameters=None, *, return_annotation=Signature.empty)?

一個 Signature 對象代表了一個函數(shù)的調(diào)用簽名和返回值標(biāo)注。對于函數(shù)接受的每個參數(shù),它對應(yīng)地在自身的 parameters 容器中存儲一個 Parameter 對象。

可選參數(shù) parameters 是一個 Parameter 對象組成的序列,它會在之后被驗證不存在名字重復(fù)的參數(shù),并且參數(shù)處于正確的順序,即僅限位置參數(shù)最前,之后緊接著可位置可關(guān)鍵字參數(shù),并且有默認(rèn)值參數(shù)在無默認(rèn)值參數(shù)之前。

可選參數(shù) return_annotation 可以是任意 Python 對象,是該可調(diào)用對象中“返回值”的標(biāo)注。

 Signature 對象是 不可變 的。使用 Signature.replace() 來構(gòu)造一個修改后的副本。

在 3.5 版更改:  Signature 對象既可封存(pickle)又可哈希。

empty?

該類的一個特殊標(biāo)記來明確指出返回值標(biāo)注缺失。

parameters?

一個參數(shù)名字到對應(yīng) Parameter 對象的有序映射。參數(shù)以嚴(yán)格的定義順序出現(xiàn),包括僅關(guān)鍵字參數(shù)。

在 3.7 版更改: Python 從 3.7 版起才顯式地保證了它保持僅關(guān)鍵字參數(shù)的定義順序,盡管實踐上在 Python 3 中一直保持了這個順序。

return_annotation?

可調(diào)用對象的“返回值”標(biāo)注。如果可調(diào)用對象沒有“返回值”標(biāo)注,這個屬性會被設(shè)置為 Signature.empty。

bind(*args, **kwargs)?

構(gòu)造一個位置和關(guān)鍵字實參到形參的映射。如果 *args**kwargs 符合簽名,則返回一個 BoundArguments;否則引發(fā)一個 TypeError 。

bind_partial(*args, **kwargs)?

Signature.bind() 作用方式相同,但允許省略部分必要的參數(shù)(模仿 functools.partial() 的行為)。返回 BoundArguments,或者在傳入?yún)?shù)不符合簽名的情況下,引發(fā)一個 TypeError。

replace(*[, parameters][, return_annotation])?

基于 replace 函數(shù)被調(diào)用的目標(biāo),創(chuàng)建一個新的 Signature 實例??梢詡鬟f不同的 parameters 和/或 return_annotation 來覆蓋原本簽名的對應(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)用對象 objSignature 對象(或其子類)。傳入 follow_wrapped=False 來直接得到 obj 的簽名,而不通過 __wrapped__ 鏈來解析包裝。在解析標(biāo)注時會將 globalnslocalns 用作命名空間。

該函數(shù)簡化了創(chuàng)建 Signature 的子類的過程:

class MySignature(Signature):
    pass
sig = MySignature.from_callable(min)
assert isinstance(sig, MySignature)

3.5 新版功能.

3.10 新版功能: 添加了參數(shù) globalnslocalns。

class inspect.Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)?

 Parameter 對象是 不可變 的。不要直接修改 Parameter 對象,而是通過 Parameter.replace() 來創(chuàng)建一個修改后的副本。

在 3.5 版更改:  Parameter 對象既可封存(pickle)又可哈希。

empty?

該類的一個特殊標(biāo)記來明確指出默認(rèn)值和標(biāo)注的缺失。

name?

參數(shù)的名字字符串。這個名字必須是一個合法的 Python 標(biāo)識符。

CPython implementation detail: CPython 會為用于實現(xiàn)推導(dǎo)式和生成器表達(dá)式的代碼對象構(gòu)造形如 .0 的隱式形參名。

在 3.6 版更改: 這些形參名會被此模塊暴露為形如 implicit0 一樣的名字。

default?

該參數(shù)的默認(rèn)值。如果該參數(shù)沒有默認(rèn)值,這個屬性會被設(shè)置為 Parameter.empty

annotation?

該參數(shù)的標(biāo)注。如果該參數(shù)沒有標(biāo)注,這個屬性會被設(shè)置為 Parameter.empty

kind?

描述實參值會如何綁定到形參。可能的取值(可以通過 Parameter 獲得,比如 Parameter.KEYWORD_ONLY ):

名稱

含意

POSITIONAL_ONLY

值必須以位置參數(shù)的方式提供。僅限位置參數(shù)是在函數(shù)定義中出現(xiàn)在 / 之前(如果有)的條目。

POSITIONAL_OR_KEYWORD

值既可以以關(guān)鍵字參數(shù)的形式提供,也可以以位置參數(shù)的形式提供(這是 Python 寫成的函數(shù)的標(biāo)準(zhǔn)綁定行為的)。

VAR_POSITIONAL

沒有綁定到其他形參的位置實參組成的元組。這對應(yīng)于 Python 函數(shù)定義中的 *args 形參。

KEYWORD_ONLY

值必須以關(guān)鍵字參數(shù)的形式提供。僅限關(guān)鍵字形參是在 Python 函數(shù)定義中出現(xiàn)在 **args 之后的條目。

VAR_KEYWORD

一個未綁定到其他形參的關(guān)鍵字參數(shù)的字典。這對應(yīng)于 Python 函數(shù)定義中的 **kwargs 形參。

例如,打印所有沒有默認(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)建一個新的 Parameter 實例。要覆寫 Parameter 的屬性,傳遞對應(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 對象當(dāng) kindPOSITIONAL_ONLY 時允許 name 被設(shè)置為 None。這現(xiàn)在已不再被允許。

class inspect.BoundArguments?

調(diào)用 Signature.bind()Signature.bind_partial() 的結(jié)果。容納實參到函數(shù)的形參的映射。

arguments?

一個形參名到實參值的可變映射。僅包含顯式綁定的參數(shù)。對 arguments 的修改會反映到 argskwargs 上。

應(yīng)當(dāng)在任何參數(shù)處理目的中與 Signature.parameters 結(jié)合使用。

備注

 Signature.bind()Signature.bind_partial() 中采用默認(rèn)值的參數(shù)被跳過。然而,如果有需要的話,可以使用 BoundArguments.apply_defaults() 來添加它們。

在 3.9 版更改:  arguments 現(xiàn)在的類型是 dict。之前,它的類型是 collections.OrderedDict

args?

位置參數(shù)的值的元組。由 arguments 屬性動態(tài)計算。

kwargs?

關(guān)鍵字參數(shù)值的字典。由 arguments 屬性動態(tài)計算。

signature?

向所屬 Signature 對象的一個引用。

apply_defaults()?

設(shè)置缺失的參數(shù)的默認(rèn)值。

對于變長位置參數(shù)(*args),默認(rèn)值是一個空元組。

對于變長關(guān)鍵字參數(shù)(**kwargs)默認(rèn)值是一個空字典。

>>>
>>> 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 新版功能.

 argskwargs 屬性可以被用于調(diào)用函數(shù):

def test(a, *, b):
    ...

sig = signature(test)
ba = sig.bind(10, b=20)
test(*ba.args, **ba.kwargs)

參見

PEP 362 - 函數(shù)簽名對象。

包含具體的規(guī)范,實現(xiàn)細(xì)節(jié)和樣例。

類與函數(shù)?

inspect.getclasstree(classes, unique=False)?

將給定的類的列表組織成嵌套列表的層級結(jié)構(gòu)。每當(dāng)一個內(nèi)層列表出現(xiàn)時,它包含的類均派生自緊接著該列表之前的條目的類。每個條目均是一個二元組,包含一個類和它的基類組成的元組。如果 unique 參數(shù)為真值,則給定列表中的每個類將恰有一個對應(yīng)條目。否則,運(yùn)用了多重繼承的類和它們的后代將出現(xiàn)多次。

inspect.getfullargspec(func)?

獲取一個 Python 函數(shù)的形參的名字和默認(rèn)值。將返回一個 具名元組

FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)

args 是一個位置參數(shù)的名字的列表。 varargs* 形參的名字或 None 表示不接受任意長位置參數(shù)時。 varkw** 參數(shù)的名字,或 None 表示不接受任意關(guān)鍵字參數(shù)。 defaults 是一個包含了默認(rèn)參數(shù)值的 n 元組分別對應(yīng)最后 n 個位置參數(shù),或 None 則表示沒有默認(rèn)值。 kwonlyargs 是一個僅關(guān)鍵詞參數(shù)列表,保持定義時的順序。 kwonlydefaults 是一個字典映射自 kwonlyargs 中包含的形參名。 annotations 是一個字典,包含形參值到標(biāo)注的映射。其中包含一個特殊的鍵 "return" 代表函數(shù)返回值的標(biāo)注(如果有的話)。

注意: signature()Signature 對象 提供可調(diào)用對象內(nèi)省更推薦的 API,并且支持?jǐn)U展模塊 API 中可能出現(xiàn)的額外的行為(比如僅限位置參數(shù))。該函數(shù)被保留的主要原因是保持兼容 Python 2 的 inspect 模塊 API。

在 3.4 版更改: 該函數(shù)現(xiàn)在基于 signature() 但仍然忽略 __wrapped__ 屬性,并且在簽名中包含綁定方法中的第一個綁定參數(shù)。

在 3.6 版更改: 該方法在 Python 3.5 中曾因 signature() 被文檔歸為棄用。但該決定已被推翻以恢復(fù)一個明確受支持的標(biāo)準(zhǔn)接口,以便運(yùn)用一份源碼通用 Python 2/3 間遺留的 getargspec() API 的遷移。

在 3.7 版更改: Python 從 3.7 版起才顯式地保證了它保持僅關(guān)鍵字參數(shù)的定義順序,盡管實踐上在 Python 3 中一直保持了這個順序。

inspect.getargvalues(frame)?

獲取傳入特定的幀的實參的信息。將返回一個 具名元組 ArgInfo(args, varargs, keywords, locals)args 是一個參數(shù)名字的列表。 varargskeyword*** 參數(shù)的名字或 None。 locals 是給定的幀的局部環(huán)境字典。

備注

該函數(shù)因疏忽在 Python 3.5 中被錯誤地標(biāo)記為棄用。

inspect.formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])?

getargvalues() 返回的四個值格式化為美觀的參數(shù)規(guī)格。 format* 的參數(shù)是對應(yīng)的可選格式化函數(shù)以轉(zhuǎn)化名字和值為字符串。

備注

該函數(shù)因疏忽在 Python 3.5 中被錯誤地標(biāo)記為棄用。

inspect.getmro(cls)?

返回由類 cls 的全部基類按方法解析順序組成的元組,包括 cls 本身。所有類不會在此元組中出現(xiàn)多于一次。注意方法解析順序取決于 cls 的類型。除非使用一個非常奇怪的用戶定義元類型,否則 cls 會是元組的第一個元素。

inspect.getcallargs(func, /, *args, **kwds)?

仿照調(diào)用方式綁定 argskwds 到 Python 函數(shù)或方法 func 參數(shù)名。對于綁定方法,也綁定第一個參數(shù)(通常命名為 self)到關(guān)聯(lián)的實例。返回一個字典,映射自參數(shù)名(包括可能存在的 *** 參數(shù))到他們對應(yīng)于 argskwds 中的值。假使 func 被錯誤地調(diào)用,即是說 func(*args, **kwds) 會因函數(shù)簽名不一致引發(fā)一個異常,那么也會引發(fā)一個相同種類的異常,并附上相同或類似的消息。例如:

>>>
>>> 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 引用的外部名字到它們的值的映射。返回一個 具名元組 ClosureVars(nonlocals, globals, builtins, unbound)。 nonlocals 映射引用的名字到詞法閉包變量,globals 映射到函數(shù)的模塊級全局, builtins 映射到函數(shù)體內(nèi)可見的內(nèi)置變量。 unbound 是在函數(shù)中引用但不能解析到給定的模塊全局和內(nèi)置變量的名字的集合。

如果 func 不是 Python 函數(shù)或方法,將引發(fā) TypeError。

3.3 新版功能.

inspect.unwrap(func, *, stop=None)?

獲取 func 所包裝的對象。它追蹤 __wrapped__ 屬性鏈并返回最后一個對象。

stop 是一個可選的回調(diào),接受包裝鏈的一個對象作為唯一參數(shù),以允許通過讓回調(diào)返回真值使解包裝更早中止。如果回調(diào)不曾返回一個真值,將如常返回鏈中的最后一個對象。例如, signature() 使用該參數(shù)來在遇到具有 __signature__ 參數(shù)的對象時停止解包裝。

如果遇到循環(huán),則引發(fā) ValueError。

3.4 新版功能.

inspect.get_annotations(obj, *, globals=None, locals=None, eval_str=False)?

計算一個對象的標(biāo)注字典。

 obj 可以是一個可調(diào)用對象、類或模塊。傳入其他類型的對象將引發(fā) TypeError。

返回一個字典。 get_annotations() 每次調(diào)用均返回一個新的字典;對同一個對象調(diào)用兩次將獲得兩個不同但相等的字典。

該函數(shù)幫助你處理若干細(xì)節(jié):

  • 如果 eval_str 為真, str 類型的值將會通過 eval() 來反字符串化。這被設(shè)計用于字符串化標(biāo)注(from __future__ import annotations)。

  • 如果 obj 不包含一個標(biāo)注字典,返回一個空字典。(函數(shù)和方法永遠(yuǎn)包含一個標(biāo)注字典;類、模塊和其他類型的可調(diào)用對象則可能沒有。)

  • 對于類,忽略繼承而來的標(biāo)注。如果一個類不包含自己的標(biāo)注字典,返回一個空字典。

  • 因安全原因,所有對于對象成員和字典值的訪問將通過 getattr()dict.get() 完成。

  • 永遠(yuǎn),永遠(yuǎn),永遠(yuǎn)返回一個新建立的字典。

eval_str 控制 str 類型的值是否應(yīng)該替換為對其調(diào)用 eval() 的結(jié)果:

  • 如果 eval_str 為真, eval() 會被調(diào)用于 str 類型。(注意 get_annotations 并不捕獲異常;如果 eval() 返回一個錯誤,它會將棧展開跳過 get_annotations 調(diào)用。)

  • 如果 eval_str 為假(默認(rèn)值), str 類型的值將不會被改變。

globalslocals 會被直接傳入函數(shù) eval(),詳見 eval() 的文檔。如果 globals 或者 localsNone,則改函數(shù)視 type(obj) 而定,可能將相應(yīng)的值替換為一個上下文有關(guān)的默認(rèn)值。

  • 如果 obj 是一個模塊, globals 默認(rèn)為 obj.__dict__。

  • 如果 obj 是一個類, globals 默認(rèn)值為 sys.modules[obj.__module__].__dict__ 并且 locals 默認(rèn)值為 obj 的類命名空間。

  • 如果 obj 是一個可調(diào)用對象, globals 默認(rèn)為 obj.__globals__,而如果 obj 是一個包裝過的函數(shù),則它會先被解包裝。

調(diào)用 get_annotations 是獲取任何對象的標(biāo)注字典的最佳實踐。關(guān)于標(biāo)注的最佳實踐的更多信息,參見 對象注解屬性的最佳實踐

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 版更改: 不再返回一個元組而是返回一個具名元組。

在 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.

備注

保留幀對象的引用(可見于這些函數(shù)返回的幀記錄的第一個元素)會導(dǎo)致你的程序產(chǎn)生循環(huán)引用。每當(dāng)一個循環(huán)引用被創(chuàng)建,所有可從產(chǎn)生循環(huán)的對象訪問的對象的生命周期將會被大幅度延長,即便 Python 的可選的循環(huán)檢測器被啟用。如果這類循環(huán)必須被創(chuàng)建,確保它們會被顯式地打破以避免對象銷毀被延遲從而導(dǎo)致占用內(nèi)存增加。

盡管循環(huán)檢測器能夠處理這種情況,這些幀(包括其局部變量)的銷毀可以通過在 finally 子句中移除循環(huán)來產(chǎn)生確定的行為。對于循環(huán)檢測器在編譯 Python 時被禁用或者使用 gc.disable() 時,這樣處理更加尤為重要。比如:

def handle_stackframe_without_leak():
    frame = inspect.currentframe()
    try:
        # do something with the frame
    finally:
        del frame

如果你希望保持幀更長的時間(比如在之后打印回溯),你也可以通過 frame.clear() 方法打破循環(huán)引用。

大部分這些函數(shù)支持的可選的 context 參數(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 版更改: 返回一個 具名元組 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 版更改: 返回一個 具名元組 FrameInfo(frame, filename, lineno, function, code_context, index) 的列表。

在 3.11 版更改: A list of FrameInfo objects is returned.

inspect.currentframe()?

返回調(diào)用者的棧幀對應(yīng)的幀對象。

CPython implementation detail: 該函數(shù)依賴于 Python 解釋器對于棧幀的支持,這并非在 Python 的所有實現(xiàn)中被保證。該函數(shù)在不支持Python 棧幀的實現(xiàn)中運(yùn)行會返回 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 版更改: 返回一個 具名元組 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 版更改: 返回一個 具名元組 FrameInfo(frame, filename, lineno, function, code_context, index) 的列表。

在 3.11 版更改: A list of FrameInfo objects is returned.

靜態(tài)地獲取屬性?

 getattr()hasattr() 都可能會在獲取或者判斷屬性是否存在時觸發(fā)代碼執(zhí)行。描述符,就和特性一樣,會被調(diào)用, __getattr__()__getattribute__() 可能會被調(diào)用。

對于你想要靜態(tài)地內(nèi)省的情況,比如文檔工具,這會顯得不方便。 getattr_static() 擁有與 getattr() 相同的簽名,但避免了獲取屬性時執(zhí)行代碼。

inspect.getattr_static(obj, attr, default=None)?

獲取屬性而不觸發(fā)描述器協(xié)議的動態(tài)查找能力 __getattr__()__getattribute__()。

注意:該函數(shù)可能無法獲取 getattr 能獲取的全部的屬性(比如動態(tài)地創(chuàng)建的屬性),并且可能發(fā)現(xiàn)一些 getattr 無法找到的屬性(比如描述器會引發(fā) AttributeError)。它也能夠返回描述器對象本身而非實例成員。

如果實例的 __dict__ 被其他成員遮蓋(比如一個特性)則該函數(shù)無法找到實例成員。

3.2 新版功能.

 getattr_static() 不解析描述器。比如槽描述器或 C 語言中實現(xiàn)的 getset 描述器。該描述器對象會被直接返回,而不處理底層屬性。

你可以用類似下方的代碼的方法處理此事。注意,對于任意 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)實現(xiàn)協(xié)程調(diào)度器或其他更高級的生成器用途時,判斷一個生成器是正在執(zhí)行、等待啟動或繼續(xù)或執(zhí)行,又或者已經(jīng)被終止是非常有用的。 getgeneratorstate() 允許方便地判斷一個生成器的當(dāng)前狀態(tài)。

inspect.getgeneratorstate(generator)?

獲取生成器迭代器的當(dāng)前狀態(tài)。

可能的狀態(tài)是:
  • GEN_CREATED:等待開始執(zhí)行。

  • GEN_RUNNING:正在被解釋器執(zhí)行。

  • GEN_SUSPENDED:當(dāng)前掛起于一個 yield 表達(dá)式。

  • GEN_CLOSED:執(zhí)行已經(jīng)完成。

3.2 新版功能.

inspect.getcoroutinestate(coroutine)?

獲取協(xié)程對象的當(dāng)前狀態(tài)。該函數(shù)設(shè)計為用于使用 async def 函數(shù)創(chuàng)建的協(xié)程函數(shù),但也能接受任何包括 cr_runningcr_frame 的類似協(xié)程的對象。

可能的狀態(tài)是:
  • CORO_CREATED:等待開始執(zhí)行。

  • CORO_RUNNING:當(dāng)前正在被解釋器執(zhí)行。

  • CORO_SUSPENDED:當(dāng)前掛起于一個 await 表達(dá)式。

  • CORO_CLOSED:執(zhí)行已經(jīng)完成。

3.5 新版功能.

生成器當(dāng)前的內(nèi)部狀態(tài)也可以被查詢。這通常在測試目的中最為有用,來保證內(nèi)部狀態(tài)如預(yù)期一樣被更新:

inspect.getgeneratorlocals(generator)?

獲取 generator 里的實時局部變量到當(dāng)前值的映射。返回一個由名字映射到值的字典。這與在生成器的主體內(nèi)調(diào)用 locals() 是等效的,并且相同的警告也適用。

如果 generator 是一個沒有關(guān)聯(lián)幀的 生成器,則返回一個空字典。如果 generator 不是一個 Python 生成器對象,則引發(fā) TypeError。

CPython implementation detail: 該函數(shù)依賴于生成器為內(nèi)省暴露一個 Python 棧幀,這并非在 Python 的所有實現(xiàn)中被保證。在這種情況下,該函數(shù)將永遠(yuǎn)返回一個空字典。

3.3 新版功能.

inspect.getcoroutinelocals(coroutine)?

該函數(shù)可類比于 getgeneratorlocals(),只是作用于由 async def 函數(shù)創(chuàng)建的協(xié)程。

3.5 新版功能.

代碼對象位標(biāo)志?

Python 代碼對象有一個 co_flags 屬性,它是下列標(biāo)志的位圖。

inspect.CO_OPTIMIZED?

代碼對象已經(jīng)經(jīng)過優(yōu)化,會采用快速局部變量。

inspect.CO_NEWLOCALS?

如果被置位,當(dāng)代碼對象被執(zhí)行時會創(chuàng)建一個新的字典作為幀的 f_locals

inspect.CO_VARARGS?

代碼對象擁有一個變長位置形參(類似 *args)。

inspect.CO_VARKEYWORDS?

代碼對象擁有一個可變關(guān)鍵字形慘死(類似 **kwrags)。

inspect.CO_NESTED?

該標(biāo)志當(dāng)代碼對象是一個嵌套函數(shù)時被置位。

inspect.CO_GENERATOR?

當(dāng)代碼對象是一個生成器函數(shù),即調(diào)用時會返回一個生成器對象,則該標(biāo)志被置位。

inspect.CO_COROUTINE?

當(dāng)代碼對象是一個協(xié)程函數(shù)時被置位。當(dāng)代碼對象被執(zhí)行時它返回一個協(xié)程。詳見 PEP 492。

3.5 新版功能.

inspect.CO_ITERABLE_COROUTINE?

該標(biāo)志被用于將生成器轉(zhuǎn)變?yōu)榛谏善鞯膮f(xié)程。包含此標(biāo)志的生成器對象可以被用于 await 表達(dá)式,并可以 yield from 協(xié)程對象。詳見 PEP 492。

3.5 新版功能.

inspect.CO_ASYNC_GENERATOR?

當(dāng)代碼對象是一個異步生成器函數(shù)時該標(biāo)志被置位。當(dāng)代碼對象被運(yùn)行時它將返回一個異步生成器對象。詳見 PEP 525。

3.6 新版功能.

備注

這些標(biāo)志特指于 CPython,并且在其他 Python 實現(xiàn)中可能從未被定義。更進(jìn)一步地說,這些標(biāo)志是一種實現(xiàn)細(xì)節(jié),并且可能在將來的 Python 發(fā)行中被移除或棄用。推薦使用 inspect 模塊的公共 API 來進(jìn)行任何內(nèi)省需求。

命令行界面?

inspect 模塊也提供一個從命令行使用基本的內(nèi)省能力。

默認(rèn)地,命令行接受一個模塊的名字并打印模塊的源代碼。也可通過后綴一個冒號和目標(biāo)對象的限定名稱來打印一個類或者一個函數(shù)。

--details?

打印特定對象的信息而非源碼。