Python Metaclass: Exploring Advanced OOP Concepts

Python Metaclass: Exploring Advanced OOP Concepts

9 mins read46 Views Comment
Updated on Oct 3, 2023 14:17 IST

In Python, everything and anything is an object, including classes. When you define a class in Python, it is actually an instance of another class called a metaclass. By default, the metaclass used in Python is the type metaclass.

2023_10_Copy-of-What-is-33.jpg

By default, the metaclass for a class is the built-in type function, which is responsible for constructing classes. However, Python allows developers to define custom metaclasses to modify or extend the behavior of class creation. Python metaclass is a class of a class that defines how a class behaves. In simpler terms, while a class defines the blueprint for creating instances (objects), a metaclass defines the blueprint for creating classes. This powerful feature, although not commonly used in everyday programming, provides a deep level of customization and control, enabling advanced object-oriented programming patterns and techniques.

Table of Contents

What is Python Metaclass?

Python metaclass is a class that defines the behaviour of other classes. It is a tool for creating and manipulating classes at runtime. Metaclasses allow you to customize the behaviour of classes. You can use metaclasses to modify the class’s attributes, add or remove methods, change the class’s inheritance hierarchy, and even customize the way the class is instantiated.

Metaprogramming with Metaclasses in Python - GeeksforGeeks

Sources: GeekforGeeeks

Note: 

The __new__() method of a metaclass is called when a new class is created. This method is responsible for creating the class object and returning it. You can modify the class’s attributes in the __new__() method.

Now let’s understand with an example code:

 
class MyMeta(type):
def __new__(cls, name, bases, attrs):
# Add a new attribute to the class
attrs['my_attr'] = 42
# Call the parent __new__ method to create the class
return super().__new__(cls, name, bases, attrs)
# Define a class that uses the metaclass
class MyClass(metaclass=MyMeta):
pass
# Access the attribute added by the metaclass
print(MyClass.my_attr) # Output: 42
Copy code
  1. In this example, we define a metaclass MyMeta as a subclass of the type metaclass. The __new__() method of MyMeta adds a new attribute my_attr to the class dictionary of attributes. 
    • We then define a new class MyClass and specify MyMeta as the metaclass.
  1. When we create an instance of MyClass, Python uses MyMeta to create and initialize the class object. The MyMeta.__new__() method is called with the arguments cls, name, bases, and attrs. 
    • These arguments are the same as those passed to the type() function when creating a class.
  1. In the __new__() method, we add a new attribute my_attr to the class dictionary of attributes and then call the type.__new__() method to create and initialize the class object. 
    • Finally, we return the class object.
  1. When we access the my_attr attribute of MyClass, Python looks it up in the class dictionary of attributes, which was modified by the MyMeta metaclass.

Metaclasses can be used to implement many advanced object-oriented programming concepts, such as creating classes dynamically, enforcing design patterns, and validating class attributes. However, metaclasses can make code more complex and harder to understand, so they should be used judiciously.

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Types of Metaclasses

1. Built-in metaclasses

These are the metaclasses that are provided by the Python language itself. The most common built-in metaclass is the type metaclass, which is used to create all classes in Python by default.

Here are some examples of built-in metaclasses in Python with detailed explanations:

  • type’ Metaclass: This is the default metaclass in Python and is used to create all classes by default. When you define a new class, Python implicitly uses the type metaclass to create the class object.

Example:

 
class MyClass:
pass
print(type(MyClass)) # <class 'type'="">
Copy code
  • ‘ABCMeta’ Metaclass: This is a special metaclass provided by the ‘abc’ module in Python and is used to define abstract base classes (ABCs). An ABC is a class that cannot be instantiated directly but can be subclassed to create concrete classes.

Example:

 
from abc import ABCMeta, abstractmethod
class MyABC(metaclass=ABCMeta):
@abstractmethod
def my_method(self):
pass
class MyConcreteClass(MyABC):
def my_method(self):
print("Hello World")
obj = MyConcreteClass()
obj.my_method() # Output: "Hello World"
Copy code

In the example above, we define an abstract base class MyABC with an abstract method my_method(). We then create a concrete class MyConcreteClass that inherits from MyABC and provides an implementation for my_method(). Since MyABC is an abstract class, it cannot be instantiated directly, but we can create an instance of MyConcreteClass and call its my_method() method.

  • EnumMeta’ Metaclass: This is a special metaclass provided by the ‘enum’ module in Python and is used to create enumeration classes. An enumeration is a set of symbolic names (members) that represent unique, constant values.

Example:

 
from enum import Enum, EnumMeta
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(type(Color)) # <enum 'enummeta'="">
Copy code

In the example above, we define an enumeration class Color with three members (RED, GREEN, and BLUE). We then use the type() function to check the type of the Color class object, which is an instance of the EnumMeta metaclass provided by the enum module.

Overall, built-in metaclasses in Python provide powerful features help in customizing the behaviour of classes and create new types of objects with unique behaviours.

2. Custom Metaclasses

These are the metaclasses that are defined by the user to create custom classes with special behavior. Custom metaclasses are typically defined by subclassing the type metaclass and overriding its methods to customize the class creation process.

Below is an example for custom metaclass:

 
class CustomMeta(type):
def __new__(cls, name, bases, attrs):
# Custom logic for class creation
# e.g., modifying attributes, adding methods, etc.
return super().__new__(cls, name, bases, attrs)
class MyClass(metaclass=CustomMeta):
pass
Copy code

In this example, CustomMeta is a custom metaclass that subclasses type. The __new__ method of the metaclass is overridden to customize the class creation process. The MyClass class is then defined using this custom metaclass.

  1. Metaclass Inheritance: Just like regular classes, metaclasses can also inherit from each other. This allows you to create a hierarchy of metaclasses, where each metaclass can provide different behaviour during class creation. Here’s an example:
 
class ParentMeta(type):
def __new__(cls, name, bases, attrs):
# Custom logic for parent metaclass
return super().__new__(cls, name, bases, attrs)
class ChildMeta(ParentMeta):
def __new__(cls, name, bases, attrs):
# Custom logic for child metaclass
return super().__new__(cls, name, bases, attrs)
class MyClass(metaclass=ChildMeta):
pass
Copy code

In this example, ParentMeta and ChildMeta are two metaclasses. ChildMeta inherits from ParentMeta and overrides its __new__ method. When the MyClass class is defined with metaclass=ChildMeta, the class creation process goes through both metaclasses, executing their respective __new__ methods in the specified order.

Real-Time Examples

1. Dynamic Model

python metaclass

In the diagram above, DynamicModelMeta is the metaclass, and DynamicModel is the class that utilizes this metaclass. When you define a new class, like User, based on DynamicModel, the metaclass DynamicModelMeta is responsible for creating and customizing the User class.

2. ORM (Object-Relational Mapping)

ORM: Rethinking Data as Objects. Object-Relational Mapping (ORM) is a… | by  Jaya | YellowAnt

Metaclasses can be used to create a mapping between Python classes and database tables in an ORM framework.

python metaclass

The metaclass handles the mapping between the class attributes and the corresponding table columns.

3. Django Model Metaclass

Django, a popular web framework, uses metaclasses extensively for its model system. Here’s an example:

 
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
Copy code

In Django, the models.Model class serves as the metaclass for creating model classes. The MyModel class defines the structure of a database table, where name and age are fields. The metaclass handles database queries, migrations, and other operations related to the model.

python metaclass

4. Validation Metaclass

Metaclasses can be used for class-level validation.

These examples demonstrate the practical use of metaclasses in real-world scenarios. Metaclasses provide a way to customize class creation, implement complex behaviour, and enforce rules or constraints at the class level.

OLD-STYLE CLASSES

In the context of metaclasses, the concept of old-style classes applies to Python 2, where metaclasses behave differently when used with old-style classes compared to new-style classes.

In Python 2, old-style classes do not invoke the metaclass when creating a new class. Instead, they use a fixed metaclass called classobj. This means that even if you define a custom metaclass, it will not be utilized when creating an old-style class.

Here’s an example to illustrate the behavior of metaclasses with old-style classes in Python 2:

 
class OldStyleClass:
pass
class Meta(type):
def __new__(cls, name, bases, attrs):
print("Metaclass invoked!")
return super().__new__(cls, name, bases, attrs)
class NewStyleClass(object):
__metaclass__ = Meta
class NewStyleClass2(metaclass=Meta):
pass
print("Creating instances:")
instance1 = OldStyleClass() # No metaclass invocation
instance2 = NewStyleClass() # Metaclass invoked!
instance3 = NewStyleClass2() # Metaclass invoked!
Copy code

In this example, we have an old-style class OldStyleClass and two new-style classes, NewStyleClass and NewStyleClass2. The Meta metaclass is defined and intended to be used for creating new-style classes.

When creating instances of the classes, you’ll notice that the metaclass is only invoked for the new-style classes (NewStyleClass and NewStyleClass2), but not for the old-style class (OldStyleClass).

With introduction of new-style classes within Python 2.2, it became the recommended approach, as new-style classes provide enhanced functionality and improved behavior with metaclasses. In Python 3, the concept of old-style classes has been removed, and all classes are implicitly new-style classes.

To summarize, in Python 2, old-style classes do not trigger the metaclass when creating a class, while new-style classes do. In Python 3, this distinction is no longer relevant as all classes are new-style classes.

NEW-STYLE CLASSES

New-style classes, also known as new-style objects, are the default class type in Python 3. They are classes that inherit from the object class explicitly or implicitly. When working with metaclasses in Python, new-style classes provide more flexibility and features compared to old-style classes.

 
class OldStyleClass:
pass
class Meta(type):
def __new__(cls, name, bases, attrs):
print("Metaclass invoked!")
return super().__new__(cls, name, bases, attrs)
class NewStyleClass(object):
__metaclass__ = Meta
class NewStyleClass2(metaclass=Meta):
pass
print("Creating instances:")
instance1 = OldStyleClass() # No metaclass invocation
instance2 = NewStyleClass() # Metaclass invoked!
instance3 = NewStyleClass2() # Metaclass invoked!
Copy code

In this example, we have an old-style class OldStyleClass and two new-style classes, NewStyleClass and NewStyleClass2. The Meta metaclass is defined and intended to be used for creating new-style classes.

When creating instances of the classes, you’ll notice that the metaclass is only invoked for the new-style classes (NewStyleClass and NewStyleClass2), but not for the old-style class (OldStyleClass).

With introduction of new-style classes within Python 2.2, it became the recommended approach, as new-style classes provide enhanced functionality and improved behavior with metaclasses. In Python 3, the concept of old-style classes has been removed, and all classes are implicitly new-style classes.

To summarize, in Python 2, old-style classes do not trigger the metaclass when creating a class, while new-style classes do. In Python 3, this distinction is no longer relevant as all classes are new-style classes.

Summary of Python Metaclass

In conclusion, Python metaclasses are a powerful feature that allows you to customize the behaviour of class creation. Metaclasses provide a way to control how classes are created, modify class attributes, and add additional functionality to classes.

Here are some key points to remember about Python metaclasses:

  • Metaclasses are classes that define how classes are created. They are responsible for controlling the class creation process.
  • The default metaclass in Python types. When you define a class without specifying a metaclass, it is created using the type metaclass.
  • You can create custom metaclasses by subclassing type and overriding its methods, particularly the __new__ method, to customize the class creation process.
  • Metaclasses can be used to enforce constraints, validate class attributes, modify class behaviour, and implement advanced object-oriented programming concepts.
  • Metaclasses can be used with new-style classes, which are the default in Python 3, and provide enhanced features and compatibility compared to old-style classes.
  • Metaclasses can be utilized to build frameworks, libraries, and systems that require custom class creation and behaviour.

Explore programming courses

It is important to note that metaclasses can be complex and should be used judiciously. They are considered an advanced topic in Python and may not be necessary in most everyday programming scenarios. However, when used appropriately, metaclasses can offer powerful capabilities for customizing and extending the Python class system.

Contributed by: Nimisha Tripathi

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio