+263773057669 info@clouditate.com

What is a python metaclass

Metaclasses in python are a useful way to describe the behavior of a class. Metaprogramming is a programming technique used in a computer software component to have the ability to treat other components as described in their data. It means that a program can be designed to understand and even change other programs or modify it’s specification while executing. Python has a perfect syntax structure and modules that support meta class definition. Many people are better off not using metaclasses in python, but if you’re using the Django web framework, metaclasses are used in model and form definition. Read more on Django metaclasses.

A meta class in python can simply be defined as a class of class that describes the behavior of the class. A class is the instance of a meta class

Lisp programming language has a reputation for having a good meta programming facility and easy syntax. I advice you check basic Lisp meta programming.

Python has old style and new style class definition syntax. Let’s explore of those class varieties:

Old style python class definition

Classes are not defined by inheriting from a base class. An instance of an old-style class is implemented from an instance. In old classes, obj.__class__ represent the class and type(obj) return the instance of the class. Here is an example:

#
#
# Old style class definition

class Hello:
   pass

obj = Hello()
obj.__class__

type(obj)

#
#
#

The old style python class definition was removed in python 3 >. Let’s look at the new style of defining classes.

New style python class definition

A class is created by inheriting from Object class. It is the only way to define a class since python 3. In the new style definition obj.__class__ is the same as type(obj). That’s the main difference between the two python class definition. Example code:

#
#
# New class definition example

class MyClass(Object):
   pass

obj = MyClass()
print(type(obj) is obj.__class__)

# end example
#
#

The above code will return true because obj.__class__ is same as type(obj).

Python classes and types

Everything in python is an object. So are classes. Let’s look at the type of class. The type of an object is a class, but the type of class is type.

Type is the metaclass and classes are the instances of the metaclass. This means that any python 3 class is an instance of the meta class.

Python classes have type as a metaclass. The metaclass is used to create classes which you’ll then use to create objects.

Creating python class dynamically

It’s possible to create python classes dynamically using the type facility. We use type and give it with three arguments, name of a class, a tuple of bases parent classes, dictionary with class attributes. Here is the example syntax:

#
#
#
Doy = type('Dog', (), {})
#
#
#

Creating custom python metaclass

Python allows a programmer to customize the class creation by passing the metaclass keyword in the class definition syntax. You can also do so by inheriting a class in which a metaclass was defined. Here is an example.

#
#
# Define Meta Class
class MyMetaClass(type):
    pass

# Define a class that takes the MyMetaClass 
class MyClass(metaclass=MyMetaClass):
    pass

# A class thatinherits a MyClass class
class MyChildclass(MyClass):
    pass

#
#
#

If you define a class and decide not to define a custom meta class, the default type is used.

__new__ and __init__ , python meta classes

You can define python metaclasses using __new__ and __init__ keywords. Let’s look at the example implimentation.

#
#
#

class MetaClass(type):
    def __new__(cls, name, bases, dict):
        pass

class AnotherMetaClass(type):
    def __init__(self, name, bases, dict):
        pass

#
#
#

__new__ is used if you want to define a dictionary (dict) or tuples before you create the class. __new__ is called after the object has been created to initialize it.

Singleton Class using a Metaclass

Singleton class is a class that can be instantiated by only one object. In python you can implement a singleton design using metaclasses. Example code.

#
#
#
class SingletonMetaClass(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(SingletonMeta,cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class MyClass(metaclass=SingletonMeta):
    pass
#
#
#

Example use of python metaclass

Python programmers can choose to use python metaclasses but the majority of people decide not to use it. However, there’re cases where your’re forced to use the metaclasses e.g. when dealing with django web framework.

Django web framework meta class

A model class is created by inheriting ModelBase class Model, the metaclass classes in the ModelBase are used to reflect database tables and we can simply use the simple syntax:

#
#
#

class User(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

#
#
#

For more information, read django meta class post, you’ll learn many meta class uses in django.

Django rest framework meta class

Django Rest Framework and its serializers use metaclasses. SerializerMetaclass creates a _declared_fields dictionary in the serializer class that contains all instances of the Field class that were included in the serializer class as attributes. Objects that implement SerializerMetaclass can then create a deep copy of these fields and implement them. Here is an example:

#
#
#

class ProfileSerializer(serializers.Serializer):
    email = serializers.EmailField()
    username = serializers.CharField(max_length=200)

#
#
#

×

Clouditate Services

×