In this article, we will show you how to add attributes to an Object in Python. You usually do that when you forget to set attributes when creating an Object or you want to add attributes to a class without needing to know it.
Add Attributes To An Object In Python
Normally, you usually create attributes in the __init__() function of the original class. So when you create a new object in the main function, attributes will be created based on the __init__() function of this object. Sometimes, you want to add attributes to an object after creating it. To do that, you can use a dot or the setattr() function in Python.
Add an attribute to an Object with a dot
In this way, after creating the object, you simply use a dot to add attributes to this object by the following command.
nameObj.nameAttr = value
Parameters
- nameObj: The name of the object.
- nameAttr: The name of the new attribute.
- value: The value of the new attribute.
Look at the example below to learn more about this solution.
class Student: def __init__(self, name='', id=''): # Student() has two attributes: name and id self.name = name self.id = id def __str__(self): return self.name + ' ' + self.id # Create the new object student = Student('Thomas Valen', 'PTIT606') print(student) # Add the attribute named 'university' with the value 'PTIT' to this student student.university = 'PTIT' print(student.university)
Output
Thomas Valen PTIT606
PTIT
Add an attribute to an Object with the setattr() function
Besides a dot, you can also use the setattr() function to add attributes to an Object in Python.
Syntax
setattr(object, name, value)
Parameters
- object: The name of the object.
- name: The name of the new attribute.
- value: The value of the new attribute.
Look at the example below to learn more about this solution.
class Student: def __init__(self, name='', id=''): # Student() has two attributes: name and id self.name = name self.id = id def __str__(self): return self.name + ' ' + self.id # Create the new object student = Student('Thomas Valen', 'PTIT606') print(student) # Add the attribute named 'university' with the value 'PTIT' to this student setattr(student, 'university', 'PTIT') print(student.university)
Output
Thomas Valen PTIT606
PTIT
Add multiple attributes to an Object with the setattr() function
You can add multiple attributes to an Object with the setattr() function.
Look at the example below.
class Student: def __init__(self, name='', id=''): # Student() has two attributes: name and id self.name = name self.id = id def __str__(self): return self.name + ' ' + self.id # Create the new object student = Student('Thomas Valen', 'PTIT606') print(student) subject = ['python', 'java', 'r'] # Add attributes as all elements in the 'subject' list for element in subject: setattr(student, element, 'A+') print(vars(student))
Output
Thomas Valen PTIT606
{'name': 'Thomas Valen', 'id': 'PTIT606', 'python': 'A+', 'java': 'A+', 'r': 'A+'}
Besides, click here if you want to check all the existing attributes in an Object.
Summary
We have shared with you how to add attributes to an Object in Python in two ways. To do that, you can use a dot or the setattr() function in Python. Note that the name of the attribute of the setattr function must be surrounded by single quotes. Thanks!

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R