Hackernoon logoBaby Steps to Metaclasses in Python3 by@keosariel

Baby Steps to Metaclasses in Python3

Author profile picture

@keosarielkenneth gabriel

Software engineer

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
str 
(
string
) and age is of type
int 
(
integer
) and however each of these variables has a property or attribute of
__class__ 
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
type(name) 
and
name.__class__
will return the same data which is
str 
(
string
) for the variable
name 
in this case.
see:
However this idea also extends to the classes you create yourself.
see:
Based on the example we see that the variable
me 
is of type
User 
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.
see:
Therefore the class
User 
is of type
type 
which means the type class was instantiated with it's expected parameter which python takes care of to create the class
User
. Moreover the parameters which the
type 
class takes are
(clsname,bases,methods)
, so lets see how it all works behind the scenes.
see:
So essentially what python does is that when you create a class it take the
clsname, bases and the functions or methods
and feeds it to the class
type
.
So why meta-classes, it turns out that the class
type 
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
see:
with that done i think we are all set!!!!!. so lets go for a test drive
see:
As we can see this works almost as exact as the class type. if you notice when creating the a new class with the
custom_type_class 
we created it had to print out a little info before making the class, which means the class was created through the
custom_type_class 
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
metaclass 
argument is the class
type
and we don't want that so we modify that by using the
custom_class_type 
we created earlier as the argument for
metaclass 
so therefore the class
User 
will be create with the
custom_class_type
.
Conclusion
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 kennethgabriel78@gmail.com. Thanks for your time bye for now :).

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!