I will start this lesson by talking about some basics of the python type system and the introduce the concept of META-CLASSES afterwards.
To start out lets look at these basic examples:
So every single thing you create in python has a type associated with it. from the example above the variable name is of type
) and age is of type
) and however each of these variables has a property or attribute of
which in most cases we call a magic method in python, this attribute is the class which is instantiated in the creation of variables and in other word it is the type of the variable. so therefore
will return the same data which is
) for the variable
in this case.
However this idea also extends to the classes you create yourself.
Based on the example we see that the variable
is of type
and the type is essentially the class we instantiated initially, so to create a new instance we use the class name and the expected parameters or arguments which will later be the associated type. However classes also have types associated with them.
Therefore the class
is of type
which means the type class was instantiated with it's expected parameter which python takes care of to create the class
. Moreover the parameters which the
class takes are
, so lets see how it all works behind the scenes.
So essentially what python does is that when you create a class it take the
and feeds it to the class
clsname, bases and the functions or methods
So why meta-classes, it turns out that the class
is actually something that you can customize and modify. Well the question is how do we do that. The way we can modify classes is through inheritance. i.e
with that done i think we are all set!!!!!. so lets go for a test drive
As we can see this works almost as exact as the class type. if you notice when creating the a new class with the
we created it had to print out a little info before making the class, which means the class was created through the
we created earlier, so therefore the class above is an example of what is called a META-CLASS.
So a simple definition of a meta-class will be:
An alternate way to monitor or supervise what happens in class definition
So what python provides is a way to use the above in class definition i.e
Basically when creating a class the default parameter for the
argument is the class
and we don't want that so we modify that by using the
we created earlier as the argument for
so therefore the class
will be create with the
And that’s all! You just learned the basics of meta-classes using python.Thanks for following along! If you liked this, give this story a couple of reactions so more people can see it!
If you LOVED this, buy me a something :) and don't forget to comment if you have questions or anything.
You can contact me by email firstname.lastname@example.org. Thanks for your time bye for now :).