尝试从.
from dataclasses import dataclass, field, InitVar
@dataclass
class XYPoint:
last_serial_no = 0
x: float
y: float = 0
skip: InitVar[int] = 1
serial_no: int = field(init=False)
def __post_init__(self, skip):
self.serial_no = self.last_serial_no + self.skip
self.__class__.last_serial_no = self.serial_no
def __add__(self, other):
new = XYPoint(self.x, self. y)
new.x += other.x
new.y += other.y
以此作为测试示例:
XYPoint.__add__(32,34)
运行代码时,出现错误: AttributeError: 'int' object has no attribute 'x' 尝试将 return 添加到 def;同样的错误。