VOOZH about

URL: https://www.geeksforgeeks.org/python/python-metaclasses/

⇱ Python Meta Classes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Meta Classes

Last Updated : 27 May, 2026

A metaclass is a class that defines how other classes are created and behave. Just like objects are created from classes, classes themselves are created from metaclasses. In short:

  • Class: creates objects
  • Metaclass: creates classes

By default, Python uses the built-in metaclass "type" to create all classes.

Steps to Create a Metaclass

  1. Define a metaclass: Create a class that inherits from type. Optionally, define __new__ or __init__ to customize class creation.
  2. Use the metaclass in a class: Specify metaclass=YourMeta when defining a class.
  3. Class is created by the metaclass: The metaclass can modify attributes or methods of the class automatically.
  4. Create instances of the class: Instances work normally, the metaclass only affects the class itself.

Output
Hello from Person!

Explanation:

  • Meta(type): Defines a metaclass that can modify classes when they are created.
  • __new__: Adds a greet method automatically to any class using this metaclass.
  • Person(metaclass=Meta): Class created using the metaclass, so it gets the greet method.
  • p = Person("Olivia"): Creates an instance of Person.
  • p.greet(): Calls the method injected by the metaclass.

Creating Subclass

Subclasses can also be created dynamically using type.


Output
Vegetarian
{'Bitter Guard', 'Spinach'}

Explanation:

  • VegType inherits from FoodType dynamically.
  • vegFoods method is added via the attributes dictionary.

Metaclass Inheritance

Metaclasses can be inherited just like normal classes. When a class inherits from a metaclass, it becomes an instance of that metaclass.


Output
<class '__main__.MetaCls'>
<class 'type'>
<class '__main__.MetaCls'>

Explanation:

  • A is created by MetaCls: type is MetaCls
  • B is normal: type is type
  • C inherits from A: type is MetaCls

Metaclass Conflicts

A class cannot inherit from two different metaclasses. Python will raise a TypeError.

Error:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Explanation:

  • Python allows only one metaclass per class.
  • 'C' inherits from two different metaclasses → conflict occurs.

Metaclass Use Cases

Metaclasses are useful when you want to control class creation or enforce rules.

1. Class Verification: This example shows how a metaclass can enforce rules on class attributes.

Explanation:

  • Ensures a class cannot have both foo and bar attributes.
  • Useful for interface enforcement.

2. Prevent Subclass Inheritance: This example shows how a metaclass can treat abstract classes differently, preventing certain checks for them while enforcing rules on normal classes.


Output
Abstract Class: AbsCls
Normal Class: NormCls

Explanation:

  • Abstract classes can skip metaclass checks.
  • Normal classes are validated by the metaclass.

Dynamic Generation of Classes

Dynamic class generation allows you to create classes at runtime instead of defining them statically in code.


Output
Food Type: Vegetarian
Food Menu: ['Spinach', 'Bitter Guard']
Food Type: Nonvegetarian
Food Menu: ['Meat', 'Fish']

Explanation:

  • sub_food dynamically creates subclasses of FoodType.
  • Each subclass can be instantiated and used like normal classes.

Related Articles:

Comment
Article Tags: