How to Add Attributes in Python Metaclass?
Last Updated :
28 Apr, 2025
This article explains what a metaclass is in the Python programming language and how to add attributes to a Python metaclass. First, let's understand what a metaclass is. This is a reasonably advanced Python topic and the following prerequisites are expected
To understand metaclasses, you need to understand Python classes. In most languages, a class is just a blueprint that describes how objects are created. In Python, classes are much more than that. Not only do classes define how instances are created, Python classes themselves are objects. Since classes are objects in Python, they are instances of a special class called metaclass and all metaclasses are instances of type class that create these class objects. Type class is the default metaclass responsible for creating classes. In simple words, Python objects are instances of a class and classes are instances of a metaclass. The following diagram summarizes this idea.
When to use a Metaclass
- If a class is defined and no metaclass is provided, the default type metaclass is used. If metaclass is specified then the base class is not an instance of type(), it will be used directly as the type for the base class.
- If you want your class to change automatically on creation, use a metaclass.
Below is a simple demonstration of how attributes can be defined and added to a Metaclass in Python.
Creating a Metaclass
To add attributes to a metaclass in Python first we need to create a metaclass, in Python, we create a metaclass in the same way as a normal class which inherits a type class. Here, a metaclass named DemoMetaClass is created which inherits the type class as shown code below:
Python3
class DemoMetaClass(type):
attr1 = 1
def __new__(cls, name, base, defcl):
obj = super().__new__(cls, name, base, defcl)
obj.attr2 = 2
return obj
- __new__(): A method called before __init__(). Creates and returns an object.
- cls: It refers to the class on which __new__ is called in.
- name: Refer to the name of the class.
- base: It is the parent class of the given class.
- defcl: It specifies some definitions for the class.
Adding Attributes to Created Metaclass
Now that we have created a metaclass called DemoMetaClass and defined attr1 and atrr2 attributes while defining it, let's create a base class that is an instance of that metaclass. As you can see we have created 2 classes first one is a default class Student and the second is Demo with metaclass as DemoMetaCLass.
Python3
# Default Class
class Student():
pass
# Base class inherited DemoMetaClass metaclass
class Demo(metaclass=DemoMetaClass):
pass
print("Student class is instance of ", type(Student))
print("Demo class is instance of ", type(Demo), end="\n\n")
print(f"DemoMetaClass \nattr1 => {Demo.attr1} \nattr2 => {Demo.attr2}\n")
Output:
The below image shows that by default the metaclass is a type class as in the case of the Student class while in the case of Demo the metaclass is DemoMetaClass, As Demo is the base class of DemoMetaClass it also possesses attributes of its metaclass, which is printed in the output.
Adding a new attribute
We can also add attributes to the metaclass as we do with the usual class in Python, As you can see in the output above a new attribute named attr3 with value 3 is been added to DemoMetaClass.
Python3
# Adding a new attribute attr3 to DemoMetaClass
DemoMetaClass.attr3 = 3
print(f"DemoMetaClass \nattr1= > {Demo.attr1} \nattr2 = > {Demo.attr2}\nattr3= > {Demo.attr3}")
Output:
Complete Code
Python3
# Custom MetaClass Defined for demo
class DemoMetaClass(type):
attr1 = 1
def __new__(cls, name, base, defcl):
obj = super().__new__(cls, name, base, defcl)
obj.attr2 = 2
return obj
# Default Class
class Student():
pass
# Base class inherited DemoMetaClass metaclass
class Demo(metaclass=DemoMetaClass):
pass
print("Student class is instance of ", type(Student))
print("Demo class is instance of ", type(Demo), end="\n\n")
print(f"DemoMetaClass \nattr1 => {Demo.attr1} \nattr2 => {Demo.attr2}\n")
# Adding a new attribute attr3 to DemoMetaClass
DemoMetaClass.attr3 = 3
print(
f"DemoMetaClass \nattr1 => {Demo.attr1} \nattr2 => {Demo.attr2}\nattr3 => {Demo.attr3}")
Output:
Student class is instance of <class 'type'>
Demo class is instance of <class '__main__.DemoMetaClass'>
DemoMetaClass
attr1 => 1
attr2 => 2
DemoMetaClass
attr1 => 1
attr2 => 2
attr3 => 3
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read