A class-of-a-class is called MetaClass. The real use of Metaclass is good to know for beginners. All languages don’t support this. But Python supports it since it follows Smalltalk. Here, you will know how to create metaclass and its use cases.

Below is the list of all sections. Go ahead and click on any of these links to go to that section.

  1. How to Identify Metaclass
  2. Why You Need Metaclass
  3. Ordinary Vs. MetaClass
  4. Use Cases of MetaClass
  5. Best Example of Python2 MetaClass
  6. Best Example of Python3 MetaClass
  7. Further Reading

How to Identify Metaclass

Example

class Meta(type):     
 pass  

# The keyword argument is 'type'
# so this is the MetaClass

class MyClass(metaclass=Meta):    
 pass

# This is not MetaClass, since, it has
# keyword argument metaclass=Meta
 
class MySubclass(MyClass):    
 pass

# This is not MetaClass, is inherited
# from MyClass.

Related Posts

Why You Need Metaclass

  • You can create MetaClass like any ordinary class
  • The Metaclass is called automatically when you call it from another class

Example:

class LittleMeta(type):
def __new__(cls, clsname, superclasses, attributedict):
print("clsname: ", clsname)
print("superclasses: ", superclasses)
print("attributedict: ", attributedict)
return type.__new__(cls, clsname, superclasses, attributedict)

We will use the metaclass "LittleMeta" in the following example:
class S:
pass
class A(S, metaclass=LittleMeta): -> called automatically
pass

a = A()
clsname: A
superclasses: (<class '__main__.S'>,)
attributedict: {'__module__': '__main__', '__qualname__': 'A'}

Ordinary Vs. Metaclass

Python uses the type function to create classes since type is actually a metaclass. The type function is the metaclass that Python uses to create class objects, but you can also create your own metaclasses.

SomeClass = MetaClass() 
object = SomeClass()
SomeClass = type('SomeClass', (), {})
How to Understand Metaclass in Python Step By Step
Python Metaclass

Featured

How to Understand Metaclass in Python Step By Step

Use Cases of Metaclass

  1. logging and profiling
  2. interface checking
  3. registering classes at creation time
  4. automatically adding new methods
  5. automatic property creation
  6. proxies
  7. automatic resource locking/synchronization.

Python2 Metaclass

In Python 2, metaclasses are set by defining the __metaclass__ variable. This variable can be any callable accepting argument, such as name, bases, and dict.

class MyBase (object):
pass

class MyMeta (type):
pass

class MyClass (MyBase):
__metaclass__ = MyMeta
pass

Python3 Metaclass

In Python 3, metaclasses are set using the keyword metaclass.

class MyBase (object):
pass

class MyMeta (type):
pass

class MyClass (MyBase, metaclass=MyMeta):
pass

Resources

Subscribe Today.

Join 1,909 other subscribers

LATEST POSTS

Secure S3 File Upload Using API Gateway, Lambda & PostgreSQL (Complete AWS Architecture Guide

Modern applications often allow users to upload files—documents, invoices, images, or datasets. But a production-grade upload pipeline must be secure, scalable, and well-organized. In this article, we will build a complete end-to-end architecture where: We will implement this using Amazon API Gateway, AWS Lambda, PostgreSQL, and Amazon S3. This architecture is widely used in cloud-native…

AI Agents in Data Engineering: Everything You Need to Know

AI agents are revolutionizing data engineering by automating tasks such as monitoring pipelines, generating SQL queries, and ensuring data quality. They enhance productivity, speed up troubleshooting, and improve data accessibility for users. While offering significant advantages, AI agents also face challenges in security, accuracy, and integration with existing systems.

The End-to-End AI Stack – A Real Guide for Developers to Code, Create, and Execute

Artificial Intelligence tools are on the rise, from writing assistants to coding helpers and automation platforms. However, many professionals struggle to compare these tools effectively. This is where the AI Stack becomes important. Modern AI tools like ChatGPT, NotebookLM, and Antigravity serve different purposes, and understanding their roles helps in: Layer 1: Conversational AI (Thinking…

10 Workplace Communication Speaking Exercises to Improve Fluency at Work

Strong workplace communication is one of the most valuable professional skills today.Whether you’re giving project updates, speaking to clients, or collaborating with teams — the ability to speak clearly and confidently can set you apart. However, many professionals struggle with: One of the best ways to improve is through chunking and pausing. Chunking helps you:✔…

Something went wrong. Please refresh the page and/or try again.