Command Palette

Search for a command to run...

Hectal
Phase 13: Python Internals and Performance
Python · Exercise 4 of 6 PreviousNext
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

  1. A plain subclass is registered under its lower-case name
  2. The name= keyword picks the key
  3. The base class is not registered
  4. A subclass of a subclass is registered too
  5. create builds an instance
  6. create raises KeyError for an unknown name
  7. 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.