← All Exercises

Python Metaclass Basics

advancedpython~35min

Background

Metaclasses are the mechanism behind class creation in Python, enabling framework-level code generation and validation.

Problem

Create a metaclass that inspects class attributes and adds a validate method checking all required fields are present and non-None.

Input

A class definition using the custom metaclass.

Output

A class with an auto-generated validate method.

Sample tests

Sample 1

Input: MyClass with fields x, y
Expected: validate() checks x and y are not None

Your solution

Learning objective

Understand metaclass __new__, class namespace manipulation, and auto-generated methods.