Is it possible to produce a namedtuple
which inherits from a base class?
What I want is that Circle
and Rectangle
are namedtuple
s and are inherited from a common base class ('Shape'):
from collections import namedtuple
class Shape:
def addToScene(self, scene):
...
Circle=namedtuple('Circle', 'x y radius')
Rectangle=namedtuple('Rectangle', 'x1 y1 x2 y2')
How would I do that?
You can try this:
class Circle(Shape, namedtuple('Circle', 'x y radius')):
pass
(You should consider adding __slots__
to all your three classes to save memory and for sightly faster lookups.)
来源:https://stackoverflow.com/questions/39098452/inherit-namedtuple-from-a-base-class-in-python