“Can't instantiate abstract class … with abstract methods” on class that shouldn't have any abstract method

前端 未结 4 1753
遇见更好的自我
遇见更好的自我 2021-01-11 14:12

Take the following minimal example:

import abc

class FooClass(object):
  __metaclass__ = abc.ABCMeta

  @abc.abstractmethod
  def FooMethod(self):
    raise         


        
4条回答
  •  被撕碎了的回忆
    2021-01-11 15:05

    Jochen is right; the abstract methods are set at class creation and won't me modified just because you reassign an attribute.

    You can manually remove it from the list of abstract methods by doing

    DerivedType.__abstractmethods__ = frozenset()
    

    or

    DerivedType.__abstractmethods__ = frozenset(
            elem for elem in DerivedType.__abstractmethods__ if elem != 'FooMethod')
    

    as well as setattr, so it doesn't still think that FooMethod is abstract.

提交回复
热议问题