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.
Soluzione
Grazie per i tuoi commenti!
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Fantastico!
Completion tasso migliorato a 4.76
Challenge: Section Review - Real-World Metaclasses
Scorri per mostrare il 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.
Soluzione
Grazie per i tuoi commenti!
single