gpt4 book ai didi

python - 无法使用参数名称调用具有单个位置参数的 Cythonized 函数

转载 作者:行者123 更新时间:2023-12-05 02:40:56 25 4
gpt4 key购买 nike

长话短说,我想对我的 python 代码进行 cython 化并构建 .so 文件以对客户隐藏它。采用这个简单的函数:

def one_positional_argument(a):
print(a)

和我的 setup.py 构建 .so 文件

from setuptools import setup, find_packages
from Cython.Build import cythonize

setup(
name='tmp',
version='1.0.0',
packages=find_packages(),
nthreads=3,
ext_modules=cythonize(
["a.py"],
compiler_directives={'language_level': 3},
build_dir="output",
),
)

当我导入 .so 文件并尝试执行我的函数时,会发生以下情况:

one_positional_argument(1) # this prints 1 and works fine
one_positional_argument(a=1) # throws TypeError: one_positional_argument() takes no keyword arguments

有多种解决方法,但我想知道我是否做错了什么

附加信息:如果我有一个带有 2 个位置参数的函数,或者一个位置参数和一个带有默认值的函数,一切正常。该问题仅存在于 1 个位置参数中。

最佳答案

您需要 always_allow_keywords 编译器指令 ( https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives )。

默认不允许它们是一种有意的兼容性/速度权衡。然而,在即将推出的 Cython v3(alpha 版本现在可用......)中,这将发生变化:https://github.com/cython/cython/issues/3090

关于python - 无法使用参数名称调用具有单个位置参数的 Cythonized 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68380123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com