Determine if a Python class is an Abstract Base Class or Concrete

后端 未结 3 1418
无人及你
无人及你 2020-12-16 09:58

My Python application contains many abstract classes and implementations. For example:

import abc
import datetime
         


        
相关标签:
3条回答
  • 2020-12-16 10:11

    You could do this with the _ast module. For example, if your example code were in foo.py you could invoked this function with "foo.py" and "FriendlyMessagePrinter" as arguments.

    def is_abstract(filepath, class_name):
        astnode = compile(open(filename).read(), filename, 'exec', _ast.PyCF_ONLY_AST)
        for node in astnode.body:
            if isinstance(node, _ast.ClassDef) and node.name == class_name:
                for funcdef in node.body:
                    if isinstance(funcdef, _ast.FunctionDef):
                        if any(not isinstance(n, _ast.Pass) for n in funcdef.body):
                            return False
                return True
        print 'class %s not found in file %s' %(class_name, filepath)
    
    0 讨论(0)
  • 2020-12-16 10:21

    Abstract classes and their concrete implementations have an __abstractmethods__ attribute containing the names of abstract methods and properties that have not been implemented. This behaviour is described in PEP 3199:

    Implementation: The @abstractmethod decorator sets the function attribute __isabstractmethod__ to the value True. The ABCMeta.__new__ method computes the type attribute __abstractmethods__ as the set of all method names that have an __isabstractmethod__ attribute whose value is true. It does this by combining the __abstractmethods__ attributes of the base classes, adding the names of all methods in the new class dict that have a true __isabstractmethod__ attribute, and removing the names of all methods in the new class dict that don't have a true __isabstractmethod__ attribute. If the resulting __abstractmethods__ set is non-empty, the class is considered abstract, and attempts to instantiate it will raise TypeError. (If this were implemented in CPython, an internal flag Py_TPFLAGS_ABSTRACT could be used to speed up this check.)

    So in concrete classes, this attribute either will not exist or will be an empty set. This is easy to check:

    def is_abstract(cls):
        if not hasattr(cls, "__abstractmethods__"):
            return False # an ordinary class
        elif len(cls.__abstractmethods__) == 0:
            return False # a concrete implementation of an abstract class
        else:
            return True # an abstract class
    

    Or more succinctly:

    def is_abstract(cls):
        return bool(getattr(cls, "__abstractmethods__", False))
    
    print(is_abstract(object))                 # False
    print(is_abstract(MessageDisplay))         # True
    print(is_abstract(FriendlyMessageDisplay)) # True
    print(is_abstract(FriendlyMessagePrinter)) # False
    
    0 讨论(0)
  • 2020-12-16 10:26
    import inspect
    print(inspect.isabstract(object))                  # False
    print(inspect.isabstract(MessageDisplay))          # True
    print(inspect.isabstract(FriendlyMessageDisplay))  # True
    print(inspect.isabstract(FriendlyMessagePrinter))  # False
    

    This checks that the internal flag TPFLAGS_IS_ABSTRACT is set in the class object, so it can't be fooled as easily as your implementation:

    class Fake:
        __abstractmethods__ = 'bluh'
    
    print(is_abstract(Fake), inspect.isabstract(Fake)) # True, False
    
    0 讨论(0)
提交回复
热议问题