Python Glossary Русский

all Python 3inspectfunction

inspect.isabstract()

object kind checks

Checks that a class is abstract: it still has unimplemented abstract methods, so it cannot be instantiated.

Syntax

inspect.isabstract(object) -> bool

It only works for classes built on ABCMeta (abc.ABC): @abstractmethod in a plain class forbids nothing, isabstract gives False and an instance is created happily. A subclass that implements every abstract method is no longer abstract. abc.ABC itself does not count as abstract, since it has no abstract methods.

Examples

import abc, inspect
class Shape(abc.ABC):
    @abc.abstractmethod
    def area(self): ...
class Square(Shape):
    def area(self): return 1
class Plain:
    @abc.abstractmethod
    def area(self): ...
print(inspect.isabstract(Shape), inspect.isabstract(Square))  # → True False
print(inspect.isabstract(Plain), type(Plain()).__name__)  # → False Plain
print(inspect.isabstract(abc.ABC))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy