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 tuples __new__ method. abcmeta.__new__ method checks abstract methods never executed (where ever is) , can't check abstract methods. instantiation not fail.

i thought inverting mro solve problem, strangely did not. i'm gonna investigate bit more , update answer.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -