Metaclass in Python and how it is helpful in framework like Django
by Akshar Raaj (speaking)
- Core Python
- Session type
- Technical level
Audience will leave this session with a better understanding of how metaprogramming and other related concepts like __new__ work in Python.
Many web frameworks written in Python, like Django, heavily use Metaclass. Understanding metaclass will help you understand the internals of your framework and how things work under the hood. Once you understand metaclass, you will have a deeper understanding of the framework you use and how Python makes complex things simpler by using metaclass.
With metaclass, things look built in rather than bolted upon.
We plan to cover following things in this talk
Talking about method __new__ for few minutes. Although my assumption will be you are familiar with __new__ .
Talking about method type() and verifying that even classes are Objects in Python.
We will define a class and verify that even this class is an Object.
class A(object): pass
We will use function type() to verify A is an Object.
Documentation of type() says that type() takes an Object as the argument and returns the class of that Object. It means that if you pass some argument to type() and type() executes successfully without raising an error, then the argument you passed is an Object
So, whatever was returned by our last statement is the class of class A and since class A was accepted by type() as an argument, A is an object.
When we see that a class is an Object in Python, we know that this class must be an instance of some other class. This 'other class' is known as metaclass.
As we know by now A is an Object, so it must be an instance of some other class. Actually, A is an instance of a class named type. Under the hood, A was created something like A=type() . Actually there were some arguments also passed in this call. So it was something like A=type(arg1, arg2, arg3, arg4). We will see these exact arguments in our talk.
So A is an instance of class type and class type is the default metaclass.
Earlier we used type() as a method to verify that even classes are objects and now we say that type is the default metaclass i.e class of a class. No need to get confused. Based on the number of arguments passed to type, it is figured out whether it needs to be used as a method or a class. We will clarify this in the talk.
Next we will write a metaclass. Here we will see what all we need to keep in mind to write a metaclass. Say we name this class as MetaClass.
- A metaclass must extend from class type.
- We need to override method __new__ of type in this metaclass.
We will keep some print statements inside overridden __new__ to see what arguments were passed to it by the interpreter. We will do some extra operations in this metaclass like adding some additional attributes on a class which is going to be created using this metaclass.
We will then write another class definition say class A. In this class definition, we will specify that the metaclass we just wrote must be used to create this class.
class A(object): a = 10 __metaclass__ = MetaClass
We will see some practical uses of Metaclass. Here we will go through a section of Django code and see how metaclass is useful in such situations.
We will see how attributes like _meta is available on any Django model we write. Actually all this is accomplished in the metaclass of Django model. We will see how keeping this _meta on model helps later while instantiating a model and saving a model.
Akshar is a developer at Agiliq. A curios person who wants to know how and why things work the way they work. He has been working on Django, a web framework written in Python, for last one year