Create Classes Dynamically in Python
Last Updated :
09 Mar, 2020
A class defines a collection of instance variables and methods to specify an object type. A class can be used to make as many object instances of the type of object as needed. An object is an identified entity with certain attributes (data members) and behaviours (member functions). Group of objects having similar characteristics and behaviour are the instance of the same class.
Python is a dynamic programming language and due to its flexibility Python has a significant advantage over statically typed languages. Python Code can be dynamically imported and classes can be dynamically created at run-time.
Classes can be dynamically created using the
type()
function in Python. The
type()
function is used to return the type of the object.
Syntax:
type(object)
The above syntax returns the type of object.
Example:
Python3 1==
# program to illustrate the use of type()
print(type("Geeks4Geeks !"))
print(type(1706256))
Output:
class 'str'
class 'int'
Creating Dynamic Classes in Python
Classes can be created dynamically using the below syntax:
Syntax:
type(name, bases, attributes)
Parameters:
name: The user defined name of the class
bases: A list of base classes, and its type is tuple
attributes: the data members and methods contained in the class
The above Syntax returns a new type of object.
Example:
Python3 1==
# program to create class dynamically
# constructor
def constructor(self, arg):
self.constructor_arg = arg
# method
def displayMethod(self, arg):
print(arg)
# class method
@classmethod
def classMethod(cls, arg):
print(arg)
# creating class dynamically
Geeks = type("Geeks", (object, ), {
# constructor
"__init__": constructor,
# data members
"string_attribute": "Geeks 4 geeks !",
"int_attribute": 1706256,
# member functions
"func_arg": displayMethod,
"class_func": classMethod
})
# creating objects
obj = Geeks("constructor argument")
print(obj.constructor_arg)
print(obj.string_attribute)
print(obj.int_attribute)
obj.func_arg("Geeks for Geeks")
Geeks.class_func("Class Dynamically Created !")
Output:
constructor argument
Geeks 4 geeks!
1706256
Geeks for GeeksClass Dynamically Created!
In the above program, class
Geeks
is dynamically created which has a constructor. The data members of
Geeks
are
string_attribute
and
int_attribute
and member functions of
Geeks
are
displayMethod()
and
classMethod()
. An object
obj
of class
Geeks
is created and all the data members are assigned and displayed, all the member functions of
Geeks
are also called.
Similar Reads
Create a directory in Python In Python, you can create directories to store and manage your data efficiently. This capability is particularly useful when building applications that require dynamic file handling, such as web scrapers, data processing scripts, or any application that generates output files.Let's discuss different
3 min read
Dynamic Attributes in Python Dynamic attributes in Python are terminologies for attributes that are defined at runtime, after creating the objects or instances. In Python we call all functions, methods also as an object. So you can define a dynamic instance attribute for nearly anything in Python. Consider the below example for
2 min read
How to Create a Dynamic Range for Loop in Python? For Loop is widely used to iterate over sequences in Python. However, there are situations where the range or sequence of the loop needs to be dynamic, and determined at runtime. In this article, we will explore various methods to achieve dynamic range for loops.Using range() with Variablesrange() f
4 min read
How to create constant in Python Python lacks built-in constant support, but constants are typically represented using all-uppercase names by convention. Although they can technically be reassigned, itâs best practice to avoid modifying their values. This convention helps indicate that such variables should remain unchanged through
2 min read
How to create a list of object in Python class In Python, creating a list of objects involves storing instances of a class in a list, which allows for easy access, modification and iteration. For example, with a Geeks class having attributes name and roll, you can efficiently manage multiple objects in a list. Let's explore different methods to
3 min read