Advanced
A plugin registry with __init_subclass__
Make the base class Reader register every subclass automatically, with no metaclass. Use __init_subclass__.
The registry is the dict Reader.registry. A subclass is stored under its class name in lower case (CsvReader becomes "csvreader"), or under the keyword given in the class line: class JsonReader(Reader, name="json"). The base class itself is not registered.
Write create(name) that returns a new instance of the registered class, and raises KeyError for an unknown name.
What Check tests
- A plain subclass is registered under its lower-case name
- The
name=keyword picks the key - The base class is not registered
- A subclass of a subclass is registered too
createbuilds an instancecreateraisesKeyErrorfor an unknown name- The program prints the sorted keys
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.