Build a Validation Metaclass
Advanced · Write the code · from Python Metaclasses: Control How Classes Are Created
From the lesson: Python Metaclasses: Control How Classes Are Created
You already know that classes create objects. But have you ever wondered - what creates the classes themselves? In Python, the answer is metaclasses.
Read the lessonCreate a metaclass called DocstringRequired that raises a TypeError with the message "Class {name} must have a docstring" if any class using it (excluding the base class) does not have a __doc__ attribute or it is None.
Then create:
Documented that uses the metaclassGoodClass with a docstring 'I have docs'Print GoodClass.__doc__ at the end.
# Create the DocstringRequired metaclass
# Create the Documented base class
# Create GoodClass with a docstring
More Metaclass exercises
- Create a Class with type()Advanced
- Predict the Metaclass OutputAdvanced
- Fix the Plugin RegistryAdvanced
- Build a Field-Discovery MetaclassAdvanced
All Python exercises · 50 practice problems · Metaprogramming quiz