如果dir()在CPython 3.4上对某些内置可调用对象(类构造函数,方法等)进行处理,则会发现其中许多通常具有一个特殊属性__text_signature__,例如:
>>> object.__text_signature__
'()'
>>> int.__text_signature__
>>> # was None
Run Code Online (Sandbox Code Playgroud)
但是,此文档不存在.此外,谷歌搜索属性名称表明还有另一个可能的特殊属性__signature__,但我没有找到任何内置函数.
我知道它们与函数参数签名有关,但除此之外,它们的值表示什么以及它们的用途是什么?
我使用 Python C API 用 C/C++ 创建了一个 Python 模块。我在 setup.py 中使用 setuptools.Extension 。
它创建一个 .py 文件,该文件从某些已编译的 .pyd 文件加载 python 模块:
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__, 'zroya.cp36-win32.pyd')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
Run Code Online (Sandbox Code Playgroud)
但它不会为 IDE 自动完成功能生成 python 存根。我希望所有导出的函数和类都可以从 .py 文件中可见:
def myfunction_stub(*args, **kwargs):
"""
... function docstring
"""
pass
Run Code Online (Sandbox Code Playgroud)
是否可以?或者我是否必须创建一些 python“预处理器”来从 .pyd 文件加载数据并生成带有文档字符串的存根?
源代码可在github上获取。
我使用.pydC++ 和pybind11. 我想.pyi为我的项目生成一个 Python 接口文件.pyd。
有几个类似的问题涉及该mypy stubgen模块,但是,这个问题会产生一个UnicodeError尝试运行文件stubgen Dummy在哪里Dummy的Dummy.pyd问题:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 2: invalid start byte
另一个项目make-stub-files根本不处理.pyd文件,给出('not a python file'文件,从而出现错误。
是否有任何工具可以让我从源.cpp文件或编译后生成 .pyi 文件.pyd文件生成 .pyi 文件?
该问题已在mypyGitHub 存储库中注册。
Python的inspect模块似乎无法检查"内置"函数的签名,其中包括C扩展模块中定义的函数,如Cython定义的函数.有没有办法获得你在这样一个模块中定义的Python函数的签名,特别是在Cython中?我希望能够找到可用的关键字参数.
MWE:
# mwe.pyx
def example(a, b=None):
pass
Run Code Online (Sandbox Code Playgroud)
和
import pyximport; pyximport.install()
import mwe
import inspect
inspect.signature(mwe.example)
Run Code Online (Sandbox Code Playgroud)
收益率:
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>
Run Code Online (Sandbox Code Playgroud)
在Python 3.4.5和Cython 0.24.1中
将Python嵌入应用程序中并编写扩展类型时,可以signature使用适当制作的.tp_doc字符串将a添加到方法中。
static PyMethodDef Answer_methods[] = {
{ "ultimate", (PyCFunction)Answer_ultimate, METH_VARARGS,
"ultimate(self, question='Life, the universe, everything!')\n"
"--\n"
"\n"
"Return the ultimate answer to the given question." },
{ NULL }
};
Run Code Online (Sandbox Code Playgroud)
当help(Answer)执行时,返回(缩写)以下内容:
class Answer(builtins.object)
|
| ultimate(self, question='Life, the universe, everything!')
| Return the ultimate answer to the given question.
Run Code Online (Sandbox Code Playgroud)
很好,但是我使用的是Python3.6,它支持注释。我想将问题注释为字符串,并返回一个int函数。我试过了:
static PyMethodDef Answer_methods[] = {
{ "ultimate", (PyCFunction)Answer_is_ultimate, METH_VARARGS,
"ultimate(self, question:str='Life, the universe, everything!') -> int\n"
"--\n"
"\n"
"Return the ultimate answer to the …Run Code Online (Sandbox Code Playgroud) python ×4
python-c-api ×2
signature ×2
cython ×1
mypy ×1
python-3.4 ×1
python-3.6 ×1
python-3.x ×1
typeshed ×1