Challenge: Section Review - Real-World Metaclasses
You have explored how metaclasses can register plugins by collecting subclasses into a registry, as well as how to enforce that certain required methods are present in class definitions. These two patterns are often combined in real-world plugin systems: you want to both track all available plugin classes and guarantee that each plugin implements a required API, such as a run method. Now you will bring these ideas together by writing a metaclass that both registers plugins and enforces that every plugin defines a run method.
Swipe to start coding
Write a metaclass that both registers plugin classes and enforces a required API. The metaclass should add every subclass of BasePlugin to a class-level list called registry, but only if the subclass defines a callable run method. If a subclass does not define a run method, your metaclass should raise a TypeError during class creation. Do not register the base class itself.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 4.76
Challenge: Section Review - Real-World Metaclasses
Glissez pour afficher le menu
You have explored how metaclasses can register plugins by collecting subclasses into a registry, as well as how to enforce that certain required methods are present in class definitions. These two patterns are often combined in real-world plugin systems: you want to both track all available plugin classes and guarantee that each plugin implements a required API, such as a run method. Now you will bring these ideas together by writing a metaclass that both registers plugins and enforces that every plugin defines a run method.
Swipe to start coding
Write a metaclass that both registers plugin classes and enforces a required API. The metaclass should add every subclass of BasePlugin to a class-level list called registry, but only if the subclass defines a callable run method. If a subclass does not define a run method, your metaclass should raise a TypeError during class creation. Do not register the base class itself.
Solution
Merci pour vos commentaires !
single