【发布时间】:2019-09-17 13:14:37
【问题描述】:
试用code from。
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;同样的错误。
【问题讨论】:
-
x: float尚未初始化。 -
XYPoint.__add__的第一个参数应该是XYPoint类型,而ints 不是很明显
标签: python python-3.x python-dataclasses