I have a class WeightedArc defined as follows:
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)
This code clearly doesn’t work, because self isn’t defined for WeightArc.__new__. How do I assign the attribute weight to the WeightArc class?
The fixed-up version of your original code is:
Another approach to look at the verbose option for collections.namedtuple to see an example of how to subclass tuple:
You can cut, paste, and modify this code, or just subclass from it as shown in the namedtuple docs.
To extend this class, build off of the fields in Arc: