Home »
Python
Python Dynamic Attributes
Last Updated : May 03, 2025
Python dynamic attributes are attributes that are added to an instance object after it has been created. In this chapter, we will learn how to add, access, and delete dynamic attributes with the help of examples.
Adding Dynamic Attributes
You can add attributes to an instance dynamically using the dot (.
) operator. These attributes will only belong to the specific instance they are added to.
Example
In this example, we add a dynamic attribute city
to an instance of the Student
class.
# Defining the class
class Student:
def __init__(self, name):
self.name = name
# Creating an instance
student1 = Student("Anjali")
# Adding dynamic attribute
student1.city = "Mumbai"
# Accessing the dynamic attribute
print(f"{student1.name} lives in {student1.city}")
The output of the above code would be:
Anjali lives in Mumbai
Dynamic Attributes Are Instance-Specific
Dynamic attributes do not affect other instances of the class. Each instance can have its own set of dynamic attributes.
Example
In this example, only one instance has the dynamic attribute city
.
# Creating another instance
student2 = Student("Ravi")
# Only student1 has 'city' attribute
print(hasattr(student1, 'city')) # True
print(hasattr(student2, 'city')) # False
The output of the above code would be:
True
False
Using setattr()
to Add Dynamic Attributes
You can also use the built-in setattr()
function to add a dynamic attribute.
Example
In this example, we add a dynamic attribute called marks
using setattr()
.
# Using setattr to add dynamic attribute
setattr(student1, 'marks', 95)
# Accessing the new attribute
print(f"{student1.name} scored {student1.marks} marks.")
The output of the above code would be:
Anjali scored 95 marks.
Using getattr()
to Access Dynamic Attributes
You can access dynamic attributes using getattr()
, which is useful when the attribute name is stored in a variable.
Example
In this example, we retrieve the value of the dynamically added marks
attribute.
# Using getattr to access the dynamic attribute
attribute_name = 'marks'
print(f"{student1.name}'s {attribute_name}: {getattr(student1, attribute_name)}")
The output of the above code would be:
Anjali's marks: 95
Deleting Dynamic Attributes
You can delete a dynamic attribute using the del
statement or the delattr()
function.
Example
In this example, we delete the marks
attribute from the instance.
# Deleting dynamic attribute using del
del student1.marks
# Checking if attribute exists
print(hasattr(student1, 'marks'))
The output of the above code would be:
False
Python Dynamic Attributes Exercise
Select the correct option to complete each statement about dynamic attributes in Python.
- In Python, dynamic attributes can be added to an object ___ it has been created.
- You can add a dynamic attribute by using the syntax object.attribute = value, where
attribute
is a ___.
- To check if an object has a dynamic attribute, you can use the ___ function.
Advertisement
Advertisement