Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pxd:我应该如何实现“\uuueq\uuuiq()”?_Python_Cython_Metaclass - Fatal编程技术网

Python pxd:我应该如何实现“\uuueq\uuuiq()”?

Python pxd:我应该如何实现“\uuueq\uuuiq()”?,python,cython,metaclass,Python,Cython,Metaclass,我试图用cython.pxd扩展现有的python源代码,Stefan Behnel在本系列的第32到35张幻灯片中对此进行了说明 作为练习的一部分,我一直在用元类中的\uuuueq\uuuu()方法碰壁。我希望我可以选择一个更简单的案例来启动Cython,但我的生产代码并没有那么简单。我编造了一个“最小的,完整的例子”来说明这个问题。。。请参阅问题底部的代码 短篇小说 如果我使用cdefinline\uuuuuu richcmp\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

我试图用cython
.pxd
扩展现有的python源代码,Stefan Behnel在本系列的第32到35张幻灯片中对此进行了说明

作为练习的一部分,我一直在用元类中的
\uuuueq\uuuu()
方法碰壁。我希望我可以选择一个更简单的案例来启动Cython,但我的生产代码并没有那么简单。我编造了一个“最小的,完整的例子”来说明这个问题。。。请参阅问题底部的代码

短篇小说

  • 如果我使用
    cdefinline\uuuuuu richcmp\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuojapan\u Car\uuuuuuuabcOther,int op:
    ,cython抱怨
  • 如果我使用
    def\uuuu richcmp\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuojapan\u Car\u ABC self,Japan\u Car\u ABC other,int op:
所以cython给了我混乱的指导

问题

  • 我知道纯python
    .pxd
    文件有限制;在my
    .pxd
    中定义
    .pxd
    是否是使用
    .pxd
    扩充纯python的有效方法
  • 如果这是使用
    .pxd
    的有效方法,如何修复此问题以正确编译
  • 如果这是错误的,那么
    .pxd
    是否可以扩展我的纯python元类,而不必在
    .pyx
    文件中重新编写整个元类?(例如:)
这是我的
.pxd

### File: car_abc.pxd
# cython: infer_types=True
cimport cython

cdef class Japan_Car_ABC:
    cpdef public char* model
    cpdef public char* color

    def __richcmp__(Japan_Car_ABC self, Japan_Car_ABC other, int op):
        """Ref: http://docs.cython.org/src/userguide/special_methods.html#rich-comparisons"""
        if op==2:
            # op==2 is __eq__() in pure python
            if self.model==other.model:
                return True
            return False
        else:
            err_msg = "op {0} isn't implemented yet".format(op)
            raise NotImplementedError(err_msg)

信息性的

car_abc.py:

### File: car_abc.py
from abc import ABCMeta

class Japan_Car_ABC(object):
    __metaclass__ = ABCMeta

    def __init__(self, model="", color=""):
        self.model = model
        self.color = color

    def __eq__(self, other):
        if self.model==other.model:
            return True
        return False

    def __hash__(self):
        return hash(self.model)
car.py:

from car_abc import Japan_Car_ABC

class Lexus(Japan_Car_ABC):
    def __init__(self, *args, **kwargs):
        bling = kwargs.pop("bling", True)     # bling keyword (default: True)
        super(Lexus, self).__init__(*args, **kwargs)
        self.bling = bling

class Toyota(Japan_Car_ABC):
    def __init__(self, *args, **kwargs):
        super(Toyota, self).__init__(*args, **kwargs)


if __name__=="__main__":
    gloria_car = Lexus(model="ES350", color="White")
    jeff_car = Toyota(model="Camry", color="Silver")
    print("gloria_car.model: {0}".format(gloria_car.model))
    print("jeff_car.model: {0}".format(jeff_car.model))
    print("gloria_car==jeff_car: {0}".format(gloria_car==jeff_car))
setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("car_abc", ["car_abc.py"]),
    #Extension("car", ["car.py"]),
    ]

setup(
    name = "really build this thing",
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)
简单的答案(从Nils Bruin通过
cython用户
)得到)是pxd不能用于实现方法(例如
\uuuu richcmp\uu()
)。因为我使用的是元类,所以我需要将代码移植到.pyx中,这样我就可以实现
\uu richcmp\uu()
和其他特殊的cython方法