inspect --- 檢查對象?

源代碼: Lib/inspect.py


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

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

類型和成員?

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

類型

屬性

描述

module -- 模塊

__doc__

文檔字符串

__file__

文件名(內置模塊沒有文件名)

class -- 類

__doc__

文檔字符串

__name__

類定義時所使用的名稱

__qualname__

qualified name -- 限定名稱

__module__

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

method -- 方法

__doc__

文檔字符串

__name__

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

__qualname__

qualified name -- 限定名稱

__func__

實現該方法的函數對象

__self__

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

__module__

定義此方法的模塊的名稱

function -- 函數

__doc__

文檔字符串

__name__

用于定義此函數的名稱

__qualname__

qualified name -- 限定名稱

__code__

包含已編譯函數的代碼對象 bytecode

__defaults__

所有位置或關鍵字參數的默認值的元組

__kwdefaults__

保存僅關鍵字參數的所有默認值的映射

__globals__

此函數定義所在的全局命名空間

__builtins__

builtins 命名空間

__annotations__

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

__module__

此函數定義所在的模塊名稱

traceback -- 回溯

tb_frame

此層的幀對象

tb_lasti

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

tb_lineno

當前行在 Python 源代碼中的行號

tb_next

下一個內部回溯對象(由本層調用)

frame -- 幀

f_back

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

f_builtins

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

f_code

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

f_globals

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

f_lasti

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

f_lineno

當前行在 Python 源代碼中的行號

f_locals

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

f_trace

此幀的追蹤函數,或``None``

code -- 代碼

co_argcount

參數數量(不包括僅關鍵字參數、* 或 ** 參數)

co_code

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

co_cellvars

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

co_consts

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

co_filename

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

co_firstlineno

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

co_flags

CO_* 標志的位圖,詳見 此處

co_lnotab

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

co_freevars

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

co_posonlyargcount

僅限位置參數的數量

co_kwonlyargcount

僅限關鍵字參數的數量(不包括 ** 參數)

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

局部變量的數量

co_stacksize

需要虛擬機堆??臻g

co_varnames

參數名和局部變量的元組

generator -- 生成器

__name__

名稱

__qualname__

qualified name -- 限定名稱

gi_frame

frame -- 幀

gi_running

生成器在運行嗎?

gi_code

code -- 代碼

gi_yieldfrom

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

coroutine -- 協(xié)程

__name__

名稱

__qualname__

qualified name -- 限定名稱

cr_await

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

cr_frame

frame -- 幀

cr_running

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

cr_code

code -- 代碼

cr_origin

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

builtin

__doc__

文檔字符串

__name__

此函數或方法的原始名稱

__qualname__

qualified name -- 限定名稱

__self__

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

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

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

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

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

inspect.getmembers(object[, predicate])?

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

備注

如果要讓 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 表示的模塊名字,但不包括外層的包名。文件擴展名會檢查是否在 importlib.machinery.all_suffixes() 列出的條目中。如果符合,則文件路徑的最后一個組成部分會去掉后綴名后被返回;否則返回``None``。

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

在 3.3 版更改: 該函數直接基于 importlib。

inspect.ismodule(object)?

當該對象是一個模塊時返回``True``。

inspect.isclass(object)?

當該對象是一個類時返回``True``,無論是內置類或者 Python 代碼中定義的類。

inspect.ismethod(object)?

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

inspect.isfunction(object)?

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

inspect.isgeneratorfunction(object)?

當該對象是一個 Python 生成器函數時返回``True``。

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

inspect.isgenerator(object)?

當該對象是一個生成器時返回``True``。

inspect.iscoroutinefunction(object)?

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

3.5 新版功能.

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

inspect.iscoroutine(object)?

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

3.5 新版功能.

inspect.isawaitable(object)?

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

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

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

3.6 新版功能.

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

inspect.isasyncgen(object)?

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

3.6 新版功能.

inspect.istraceback(object)?

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

inspect.isframe(object)?

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

inspect.iscode(object)?

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

inspect.isbuiltin(object)?

如果該對象是一個內置函數或一個綁定的內置方法,則返回 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)?

如果該對象是一個用戶定義的或內置的函數或者方法,則返回 True。

inspect.isabstract(object)?

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

inspect.ismethoddescriptor(object)?

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

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

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

inspect.isdatadescriptor(object)?

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

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

inspect.isgetsetdescriptor(object)?

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

CPython implementation detail: getset 是在擴展模塊中通過 PyGetSetDef 結構體定義的屬性。對于不包含這種類型的 Python 實現,這個方法將永遠返回 False

inspect.ismemberdescriptor(object)?

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

CPython implementation detail: 成員描述器是在擴展模塊中通過 PyMemberDef 結構體定義的屬性。對于不包含這種類型的 Python 實現,這個方法將永遠返回 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 版更改: 文檔字符串沒有被重寫的話現在會被繼承。

inspect.getcomments(object)?

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

inspect.getfile(object)?

返回定義了這個對象的文件名(包括文本文件或二進制文件)。如果該對象是一個內置模塊、類或函數則會失敗并引發(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)?

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

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

inspect.getsource(object)?

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

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

inspect.cleandoc(doc)?

清理文檔字符串中為對齊當前代碼塊進行的縮進

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

使用 Signature 對象對可調用對象進行內省?

3.3 新版功能.

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

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 可調用對象,包括單純的函數、類,到 functools.partial() 對象。

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

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

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

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

3.10 新版功能: 添加參數 globals 、 localseval_str

備注

一些可調用對象可能在特定 Python 實現中無法被內省。例如,在 CPython 中,部分通過 C 語言定義的內置函數不提供關于其參數的元數據。

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

一個 Signature 對象代表了一個函數的調用簽名和返回值標注。對于函數接受的每個參數,它對應地在自身的 parameters 容器中存儲一個 Parameter 對象。

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

可選參數 return_annotation 可以是任意 Python 對象,是該可調用對象中“返回值”的標注。

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

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

empty?

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

parameters?

一個參數名字到對應 Parameter 對象的有序映射。參數以嚴格的定義順序出現,包括僅關鍵字參數。

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

return_annotation?

可調用對象的“返回值”標注。如果可調用對象沒有“返回值”標注,這個屬性會被設置為 Signature.empty。

bind(*args, **kwargs)?

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

bind_partial(*args, **kwargs)?

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

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

基于 replace 函數被調用的目標,創(chuàng)建一個新的 Signature 實例。可以傳遞不同的 parameters 和/或 return_annotation 來覆蓋原本簽名的對應的屬性。要從復制的 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)?

返回給定可調用對象 objSignature 對象(或其子類)。傳入 follow_wrapped=False 來直接得到 obj 的簽名,而不通過 __wrapped__ 鏈來解析包裝。在解析標注時會將 globalnslocalns 用作命名空間。

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

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

3.5 新版功能.

3.10 新版功能: 添加了參數 globalnslocalns。

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

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

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

empty?

該類的一個特殊標記來明確指出默認值和標注的缺失。

name?

參數的名字字符串。這個名字必須是一個合法的 Python 標識符。

CPython implementation detail: CPython 會為用于實現推導式和生成器表達式的代碼對象構造形如 .0 的隱式形參名。

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

default?

該參數的默認值。如果該參數沒有默認值,這個屬性會被設置為 Parameter.empty

annotation?

該參數的標注。如果該參數沒有標注,這個屬性會被設置為 Parameter.empty 。

kind?

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

名稱

含意

POSITIONAL_ONLY

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

POSITIONAL_OR_KEYWORD

值既可以以關鍵字參數的形式提供,也可以以位置參數的形式提供(這是 Python 寫成的函數的標準綁定行為的)。

VAR_POSITIONAL

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

KEYWORD_ONLY

值必須以關鍵字參數的形式提供。僅限關鍵字形參是在 Python 函數定義中出現在 **args 之后的條目。

VAR_KEYWORD

一個未綁定到其他形參的關鍵字參數的字典。這對應于 Python 函數定義中的 **kwargs 形參。

例如,打印所有沒有默認值的僅關鍵字參數:

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

例子:打印所有參數的描述:

>>>
>>> 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 函數被調用的目標,創(chuàng)建一個新的 Parameter 實例。要覆寫 Parameter 的屬性,傳遞對應的參數。要移除 Parameter 的默認值和/或標注,傳遞 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 對象當 kindPOSITIONAL_ONLY 時允許 name 被設置為 None。這現在已不再被允許。

class inspect.BoundArguments?

調用 Signature.bind()Signature.bind_partial() 的結果。容納實參到函數的形參的映射。

arguments?

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

應當在任何參數處理目的中與 Signature.parameters 結合使用。

備注

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

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

args?

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

kwargs?

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

signature?

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

apply_defaults()?

設置缺失的參數的默認值。

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

對于變長關鍵字參數(**kwargs)默認值是一個空字典。

>>>
>>> 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 屬性可以被用于調用函數:

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

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

參見

PEP 362 - 函數簽名對象。

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

類與函數?

inspect.getclasstree(classes, unique=False)?

將給定的類的列表組織成嵌套列表的層級結構。每當一個內層列表出現時,它包含的類均派生自緊接著該列表之前的條目的類。每個條目均是一個二元組,包含一個類和它的基類組成的元組。如果 unique 參數為真值,則給定列表中的每個類將恰有一個對應條目。否則,運用了多重繼承的類和它們的后代將出現多次。

inspect.getfullargspec(func)?

獲取一個 Python 函數的形參的名字和默認值。將返回一個 具名元組

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

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

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

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

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

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

inspect.getargvalues(frame)?

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

備注

該函數因疏忽在 Python 3.5 中被錯誤地標記為棄用。

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

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

備注

該函數因疏忽在 Python 3.5 中被錯誤地標記為棄用。

inspect.getmro(cls)?

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

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

仿照調用方式綁定 argskwds 到 Python 函數或方法 func 參數名。對于綁定方法,也綁定第一個參數(通常命名為 self)到關聯的實例。返回一個字典,映射自參數名(包括可能存在的 *** 參數)到他們對應于 argskwds 中的值。假使 func 被錯誤地調用,即是說 func(*args, **kwds) 會因函數簽名不一致引發(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 函數或方法 func 引用的外部名字到它們的值的映射。返回一個 具名元組 ClosureVars(nonlocals, globals, builtins, unbound)。 nonlocals 映射引用的名字到詞法閉包變量,globals 映射到函數的模塊級全局, builtins 映射到函數體內可見的內置變量。 unbound 是在函數中引用但不能解析到給定的模塊全局和內置變量的名字的集合。

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

3.3 新版功能.

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

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

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

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

3.4 新版功能.

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

計算一個對象的標注字典。

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

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

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

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

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

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

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

  • 永遠,永遠,永遠返回一個新建立的字典。

eval_str 控制 str 類型的值是否應該替換為對其調用 eval() 的結果:

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

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

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

  • 如果 obj 是一個模塊, globals 默認為 obj.__dict__。

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

  • 如果 obj 是一個可調用對象, globals 默認為 obj.__globals__,而如果 obj 是一個包裝過的函數,則它會先被解包裝。

調用 get_annotations 是獲取任何對象的標注字典的最佳實踐。關于標注的最佳實踐的更多信息,參見 對象注解屬性的最佳實踐。

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.

備注

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

盡管循環(huán)檢測器能夠處理這種情況,這些幀(包括其局部變量)的銷毀可以通過在 finally 子句中移除循環(huá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)引用。

大部分這些函數支持的可選的 context 參數指定返回時包含的上下文的行數,以當前行為中心。

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()?

返回調用者的棧幀對應的幀對象。

CPython implementation detail: 該函數依賴于 Python 解釋器對于棧幀的支持,這并非在 Python 的所有實現中被保證。該函數在不支持Python 棧幀的實現中運行會返回 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í)行。描述符,就和特性一樣,會被調用, __getattr__()__getattribute__() 可能會被調用。

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

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

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

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

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

3.2 新版功能.

 getattr_static() 不解析描述器。比如槽描述器或 C 語言中實現的 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é)程的當前狀態(tài)?

當實現協(xié)程調度器或其他更高級的生成器用途時,判斷一個生成器是正在執(zhí)行、等待啟動或繼續(xù)或執(zhí)行,又或者已經被終止是非常有用的。 getgeneratorstate() 允許方便地判斷一個生成器的當前狀態(tài)。

inspect.getgeneratorstate(generator)?

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

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

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

  • GEN_SUSPENDED:當前掛起于一個 yield 表達式。

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

3.2 新版功能.

inspect.getcoroutinestate(coroutine)?

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

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

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

  • CORO_SUSPENDED:當前掛起于一個 await 表達式。

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

3.5 新版功能.

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

inspect.getgeneratorlocals(generator)?

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

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

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

3.3 新版功能.

inspect.getcoroutinelocals(coroutine)?

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

3.5 新版功能.

代碼對象位標志?

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

inspect.CO_OPTIMIZED?

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

inspect.CO_NEWLOCALS?

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

inspect.CO_VARARGS?

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

inspect.CO_VARKEYWORDS?

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

inspect.CO_NESTED?

該標志當代碼對象是一個嵌套函數時被置位。

inspect.CO_GENERATOR?

當代碼對象是一個生成器函數,即調用時會返回一個生成器對象,則該標志被置位。

inspect.CO_COROUTINE?

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

3.5 新版功能.

inspect.CO_ITERABLE_COROUTINE?

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

3.5 新版功能.

inspect.CO_ASYNC_GENERATOR?

當代碼對象是一個異步生成器函數時該標志被置位。當代碼對象被運行時它將返回一個異步生成器對象。詳見 PEP 525。

3.6 新版功能.

備注

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

命令行界面?

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

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

--details?

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