我有一个class WeightedArc定义如下:
class Arc(tuple):
@property
def tail(self):
return self[0]
@property
def head(self):
return self[1]
@property
def inverted(self):
return Arc((self.head, self.tail))
def __eq__(self, other):
return self.head == other.head and self.tail == other.tail
class WeightedArc(Arc):
def __new__(cls, arc, weight):
self.weight = weight
return super(Arc, cls).__new__(arc)
这段代码显然行不通,因为self
没有为WeightArc.__new__
定义。如何将属性weight赋给WeightArc类?在
更好的是,为什么不使用namedtuples我们自己呢?:)
原始代码的修正版本是:
另一种查看详细选项的方法collections.namedtuple查看如何子类元组的示例:
^{pr2}${你可以从这个子类中剪切},或者粘贴它。在
要扩展此类,请在Arc中构建字段:
相关问题 更多 >
编程相关推荐