How to use metaclass in Python with examples

In this tutorial, We will discuss what is Metaclass in Python Language. Basically, metaclass defines class behaviors. Let’s dive in with some simple examples to have a clear idea about these metaclasses.

Metaclasses in Python:

Python is an Object-oriented Programming language, A metaclass is a class whose instances are classes. It defines the behavior of certain classes and their instances. In Python, everything is an object.  It defines the classes of built-in classes and Non-built-in functions.

Let’s see with Examples,

For Non built-in Functions

Here is the sample code

Python = 'b'
Lan = {'y' : 4, 'z' : 5}
class fun:
   pass
F = fun()
for obj in (Python, Lan, F):
  print(type(obj) is obj.__class__)

Output:

True

True

True

For built-in Functions

This is the sample code

Python = [int, float, list, tuple]

for Lan in Python:
   print(type(Lan))

 

Output:

<class 'type'>

<class 'type'>

<class 'type'>

<class 'type'>

You may also read:

Understanding self in Python class

Data Classes in Python

What is Metaclass in Python

Leave a Reply

Your email address will not be published. Required fields are marked *