Class attribute considered abstract method in python 2.7 - abc module

I am trying to implement an abstract superclass (Base) with an abstract method (addfeature) that 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 doesn't work however

"TypeError: unable to instantiate abstract class Child with abstract methods em"

Why? em should be a class attribute, not a method (and certainly not an abstract method)

+3


source to share


1 answer


ABCMeta

check if a method is abstract or not using an attribute __isabstractmethod__

. lxml.builder.ElementMaker

dynamically generates a method (using __getattr__

); access to __isabstractmethod__

confused ABCMeta

.

>>> ns = "http://www.foo.com/bar"
>>> em = ElementMaker(namespace=ns, nsmap={'bar': ns})
>>> em.child_element
<functools.partial object at 0x0000000002B55598>
>>> em.child_element()
<Element {http://www.foo.com/bar}child_element at 0x27a8828>
>>> em.__isabstractmethod__
<functools.partial object at 0x0000000002B55598>
>>> bool(em.__isabstractmethod__)
True

      



By assigning __isabstractmethod__

how False

, you can get around it.

class Base(object):
    __metaclass__ = ABCMeta

    ns = "http://www.foo.com/bar"
    em = ElementMaker(namespace=ns, nsmap={'bar': ns})
    em.__isabstractmethod__ = False  # <-----

    @abstractmethod
    def addfeature(self):
        pass

      

+3


source







All Articles