Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Section Review - Real-World Metaclasses | Metaclasses in Real-World Scenarios
Python Metaclasses Demystified

bookChallenge: 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.

Aufgabe

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.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 7
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookChallenge: Section Review - Real-World Metaclasses

Swipe um das Menü anzuzeigen

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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 7
single

single

some-alt