0

刚开始学习attrsdataclass

Shape我有以下Rectangle课程来说明我的问题。在我覆盖rect_length_default子类中的实现之前,代码工作正常。

我的问题:为default超类中定义的属性覆盖修饰方法的正确方法是什么?

import attr

@attr.s(auto_attribs=True)
class Shape(object):
  length: int = attr.ib()

  @length.default
  def rect_length_default(self):
    return 0


@attr.s(auto_attribs=True)
class Rectangle(Shape):
  def rect_length_default(self):
    return 100


print(Shape(5))
print(Shape())
print(Rectangle())
print(Rectangle(15))

输出:

Shape(length=5)
Shape(length=0)
Rectangle(length=0)  # I was expecting it to be 100
Rectangle(length=15)
4

0 回答 0