Skip to main content
 首页 » 编程设计

python之在 cython 扩展中正确覆盖 __rmul__

2025年05月04日15freeliver54

我必须如何在 cython 中覆盖 rmul?

例如,这在 python 中完美运行

class PyFoo: 
  def __init__(self): 
      self.a = 4 
  def __mul__(self,b):  return b*self.a 
  def __rmul__(self,b): return self*b 
 
Pynew = PyFoo() 
 
print "   Python   " 
print Pynew*3 # I got 12 
print 3*Pynew # I got 12 

但是,如果我在 Cython 中实现相同的功能则行不通,

cclass.pyx

cimport cython 
 
cdef class Foo: 
  cdef public int a 
  def __init__(self): 
      self.a = 4 
  def __mul__(self,b):  return b*self.a 
  def __rmul__(self,b): return self*b 

测试.py

import cclass as c 
Cnew = c.Foo() 
print "   Cython   " 
print Cnew*3 # This works, I got 12 
print 3*Cnew # This doesn't 

我遇到了这个错误

    Traceback (most recent call last): 
  File "test.py", line 22, in <module> 
    print 3*Cnew 
  File "cclass.pyx", line 8, in cclass.Foo.__mul__ (cclass.c:763) 
    def __mul__(self,b):  return b*self.a 
AttributeError: 'int' object has no attribute 'a' 

我不明白在 Cython 中使用相同的 rmul 实现有什么问题。

请您参考如下方法:

这是不看文档的情况。在Special Methods of Extension Types用户指南,您会发现以下内容:

Arithmetic operator methods, such as __add__(), behave differently from their Python counterparts. There are no separate “reversed” versions of these methods (__radd__(), etc.) Instead, if the first operand cannot perform the operation, the same method of the second operand is called, with the operands in the same order.

This means that you can’t rely on the first parameter of these methods being “self” or being the right type, and you should test the types of both operands before deciding what to do. If you can’t handle the combination of types you’ve been given, you should return NotImplemented.

所以你真的应该做一些类型检查,至少通过以下方式:

cdef class Foo: 
    cdef public int a 
 
    def __init__(self): 
        self.a = 4 
 
    def __mul__(first, other): 
        if isinstance(first, Foo): 
            return first.a * other 
        elif isinstance(first, int): 
            return first * other.a 
        else: 
            return NotImplemented 

此解决方案对于 Foo 类的使用过于乐观,您可能还需要检查 other 的类型,和/或检查更通用的 < em>数字类型。