[issue43923] Can't create generic NamedTuple as of py3.9

Serhiy Storchaka report at bugs.python.org
Thu Mar 10 03:17:42 EST 2022


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

What about mix-ins or abstract base classes?

class VecMixin:
    def length(self):
        return math.hypot(*self)

class Vec2D(NamedTuple, VecMixin):
    x: float
    y: float

class Vec3D(NamedTuple, VecMixin):
    x: float
    y: float
    z: float

Currently you need to use the following trick to get a similar result:

class Vec2D(NamedTuple):
    x: float
    y: float

class Vec2D(Vec2D, VecMixin):
    pass

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43923>
_______________________________________


More information about the Python-bugs-list mailing list