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

Task

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 7
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookChallenge: Section Review - Real-World Metaclasses

Swipe to show 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.

Task

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 7
single

single

some-alt