Python Glossary Русский

Python 3.10+abcfunction

abc.update_abstractmethods

abc utilities

Recomputes a class's set of abstract methods (e.g. after methods are added dynamically; Python 3.10+).

Syntax

abc.update_abstractmethods(cls)

A class's __abstractmethods__ set is computed once, at class creation, so anything that adds or replaces methods afterwards (a class decorator, a mixin, a plain assignment) leaves it stale: the class either instantiates happily with unimplemented methods or refuses to instantiate although everything is implemented. It returns the same class object, which makes it convenient to apply as a decorator.

Examples

import abc
class C: pass
print(abc.update_abstractmethods(C) is C)   # → True
import abc
class Base(abc.ABC): pass
Base.run = abc.abstractmethod(lambda self: None)
print(abc.update_abstractmethods(Base).__abstractmethods__)   # → frozenset({'run'})

See also

Open in the full glossarydocs.python.orgReport an inaccuracy