python - Mix-in of abstract class and namedtuple -
i want define mix-in of namedtuple , base class defines , abstract method:
import abc import collections class a(object): __metaclass__ = abc.abcmeta @abc.abstractmethod def do(self): print("u can't touch this") b = collections.namedtuple('b', 'x, y') class c(b, a): pass c = c(x=3, y=4) print(c) c.do()
from understand reading docs , other examples have seen, c.do()
should raise error, class c not implement do()
. however, when run it... works:
b(x=3, y=4) u can't touch
i must overlooking something.
when take @ method resolution order of c
see b
comes before a
in list. means when instantiate c
__new__
method of b
called first.
this implementation of namedtuple.__new__
def __new__(_cls, {arg_list}): 'create new instance of {typename}({arg_list})' return _tuple.__new__(_cls, ({arg_list}))
you can see not support cooperative inheritance, because breaks chain , calls tuple
s __new__
method. method checks abstract methods never executed (where ever is) , can't check abstract methods. instantiation not fail.abcmeta.__new__
i thought inverting mro solve problem, strangely did not. i'm gonna investigate bit more , update answer.
Comments
Post a Comment