This is what 99% Pythonistas might not know !

Abin Isaac
3 min readSep 25, 2021

Lets create a Class Factory !!

Creating a Class factory !
assembly_line.gif (480×270) (biol355.github.io)

Meta-Class

Have you ever seen this term while coding ?

Normally people don't know about Meta class and even if they know they don't bother to dig into it !

First of all lets write some code to see this one and only meta class in python

class my_class(metaclass=type):
pass

This runs perfectly like a normal class syntax !!

“class type” is the only one meta class in python 3 and every other class (built-in or custom) in python is of type <class type>

Come, Lets check type of classes in Python!

Finding built-in class types -

for i in int, float, dict, list, tuple:
print(type(i))
OUTPUT<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>

and now custom class type check :-

class A:
pass
print(type(a))
OUTPUT<class 'type'>

and at last lets check the type of class type :-

print(type(type))OUTPUT<class 'type'>

Even class type is an instance of itself !!!

Creating a class using Type function

Other than returning type of different objects (class is also an object) type function has a different syntax also which is used to define a class.

Type function syntax to define a classtype(name, bases, dict)name: Required. A class name.bases: Required. A tuple that itemizes the base class.dict: Required. A dictionary which is the namespace containing definitions for the class body.

Lets, write an empty class using Type Function

#Creating an empty class using Type function My_class_var = type(‘My_class’, (), {})
#Normal way to create an empty classclass My_normal_class:
pass

Both the above ways create empty classes!

Now, lets add some functions in the class

# Creating class with an attribute using Type functiondef fun(obj):
print("Learning Metaclass")
My_class_var = type('My_class', (object,), {
'attr1' : 100,
'attr2':lambda x : x.attr1

'attr3' : fun
})My_class_obj = My_class_var()
print(My_class_obj.attr1)
print(My_class_obj.attr2())
print(My_class_obj.attr3())
OUTPUT100
100
Learning Metaclass

Why Meta Class ?

In the same way Class is used to create objects Meta class acts like a blue print for classes and can be used to create number of classes.

In Python every class is an Object-factory and every Meta class is a Class-factory.

Meta Class comes in python meta programming along with Decorators and Class-decorators which is used for Code — Reusability .

Tim Peters, Author of Zen of Python explains meta class like below

“Metaclasses are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).”

Creating a custom Meta-Class

Till now we have discussed a lot about the meta classes and now lets make one to create a Class Factory for ourselves !!

We just need to create a class inheriting type class and edit its __new__ method with the attributes which we want to see in our classes

# Creating our Meta Classclass My_Class_factory(type):  def __new__(cls, name, bases, dct):     x = super().__new__(cls, name, bases, dct)     x.name = “Asd”     x.age=123     return x
# Creating first class using this custom meta class class Class1(metaclass=My_Class_factory): passobj1= Class1()print(obj1.name)
# Creating second class using this custom meta classclass Class2(metaclass=My_Class_factory): passobj2= Class2()print(obj2.name)
# Creating third class using this custom meta classclass Class3(metaclass=My_Class_factory): passobj3= Class3()print(obj3.name)OUTPUTAsd
Asd
Asd

That's all!

Hope you now we got a basic idea about Python Meta-Classes, their need and the way to create a Class Factory !

Happy Learning !

--

--