I am trying to implement an abstract superclass (Base) with an abstract method (addfeature), which the Child class will override.
from lxml.builder import ElementMaker
from abc import ABCMeta, abstractmethod
class Base(object):
__metaclass__ = ABCMeta
ns = "http://www.foo.com/bar"
em = ElementMaker(namespace=ns, nsmap={'bar': ns})
@abstractmethod
def addfeature(self):
pass
class Child(Base):
def addfeature(self):
pass
child_instance = Child()
This code fails however with
"TypeError: Can't instantiate abstract class Child with abstract methods em"
Why? em is supposed to be a class attribute, not a method (and surely not an abstract method)
ABCMeta
check whether the method is abstract or not by using__isabstractmethod__
attribute. Thelxml.builder.ElementMaker
dynamically generate a method (using__getattr__
); access to__isabstractmethod__
confuse theABCMeta
.By assigning
__isabstractmethod__
as aFalse
, you can work around it.