Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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.

Opgave

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øsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 7
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Section Review - Real-World Metaclasses

Stryg for at vise menuen

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.

Opgave

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øsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 7
single

single

some-alt