Python的inspect
模塊似乎無法檢查「內置」函數的簽名,其中包含C擴展模塊中定義的函數,如用Cython。有什麼辦法可以獲得你在這個模塊中定義的Python函數的簽名,特別是在Cython中?我期望能夠找到可用的關鍵字參數。如何內省在Cython C擴展模塊中定義的函數
MWE:
# mwe.pyx
def example(a, b=None):
pass
和
import pyximport; pyximport.install()
import mwe
import inspect
inspect.signature(mwe.example)
產量:
Traceback (most recent call last):
File "mwe_py.py", line 5, in <module>
inspect.signature(mwe.example)
File "/nix/store/134l79vxb91w8mhxxkj6kb5llf7dmwpm-python3-3.4.5/lib/python3.4/inspect.py", line 2063, in signature
return _signature_internal(obj)
File "/nix/store/134l79vxb91w8mhxxkj6kb5llf7dmwpm-python3-3.4.5/lib/python3.4/inspect.py", line 1965, in _signature_internal
skip_bound_arg=skip_bound_arg)
File "/nix/store/134l79vxb91w8mhxxkj6kb5llf7dmwpm-python3-3.4.5/lib/python3.4/inspect.py", line 1890, in _signature_from_builtin
raise ValueError("no signature found for builtin {!r}".format(func))
ValueError: no signature found for builtin <built-in function example>
在Python 3.4.5,並用Cython 0.24.1
爲什麼不能這樣做? 'inspect.signature(all)'(例如提取['all']的簽名)(https://docs.python.org/library/functions.html#all))很好用''。請提供一個[mcve],以便答案可以實際向您展示如何在*您的案例*中做到這一點。 –
MSeifert
[Python inspect.getargspec with built-in function]的可能重複](https://stackoverflow.com/questions/11343191/python-inspect-getargspec-with-built-in-function) – DavidW
也適用:https:// stackoverflow.com/questions/1104823/python-c-extension-method-signatures-for-documentation – DavidW