How to Add attributes to an Object in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Add attributes to an Object in Python
  2. Add attribute to an Object using dot notation
  3. Add multiple attributes to an object using a for loop
  4. Add attributes to an Object using SimpleNamespace()

# Add attributes to an Object in Python

Use the setattr() function to add attributes to an object, e.g. setattr(obj, 'name', 'value').

The setattr function takes an object, the name of the attribute and its value as arguments and adds the specified attribute to the object.

main.py
class Employee(): def __init__(self, name): self.name = name emp1 = Employee('bobby') setattr(emp1, 'salary', 100) setattr(emp1, 'age', 30) print(getattr(emp1, 'salary')) # ๐Ÿ‘‰๏ธ 100 print(getattr(emp1, 'age')) # ๐Ÿ‘‰๏ธ 30 print(getattr(emp1, 'name')) # ๐Ÿ‘‰๏ธ bobby

add attributes to an object

The code for this article is available on GitHub

We instantiated the Employee() class and used the setattr() function to add attributes to the object.

The setattr() function adds an attribute to an object.

The function takes the following 3 arguments:

NameDescription
objectthe object to which the attribute is added
namethe name of the attribute
valuethe value of the attribute

The name string may be an existing or a new attribute.

If you need to create a generic object, use the pass statement in a class.

main.py
class GenericClass(): pass obj1 = GenericClass() setattr(obj1, 'salary', 100) setattr(obj1, 'age', 30) print(getattr(obj1, 'salary')) # ๐Ÿ‘‰๏ธ 100 print(getattr(obj1, 'age')) # ๐Ÿ‘‰๏ธ 30

creating generic object

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

Make sure your class does not extend from the built-in object class.

The object class doesn't have a __dict__ attribute, therefore we can't assign attributes to an instance of the class.

# Add attribute to an Object using dot notation

You can also use dot notation to add an attribute to an object.

main.py
class GenericClass(): pass obj1 = GenericClass() obj1.salary = 100 obj1.age = 30 print(getattr(obj1, 'salary')) # ๐Ÿ‘‰๏ธ 100 print(getattr(obj1, 'age')) # ๐Ÿ‘‰๏ธ 30 print(obj1.salary) # ๐Ÿ‘‰๏ธ 100 print(obj1.age) # ๐Ÿ‘‰๏ธ 30

add attribute to object using dot notation

The code for this article is available on GitHub

Using dot notation is equivalent to using the setattr() method.

However, when using dot notation, you might get linting warnings for defining attributes outside of the __init__() method.

# Add multiple attributes to an object using a for loop

You can use a for loop if you need to add multiple attributes to an object.

main.py
class GenericClass(): pass my_dict = {'name': 'bobbyhadz', 'age': 30} obj1 = GenericClass() for key, value in my_dict.items(): setattr(obj1, key, value) print(getattr(obj1, 'name')) # ๐Ÿ‘‰๏ธ bobbyhadz print(getattr(obj1, 'age')) # ๐Ÿ‘‰๏ธ 30

add multiple attributes to object using for loop

The code for this article is available on GitHub

We used a for loop to iterate over a dictionary's items and added the key-value pairs as attributes to the object.

If you have to use the setattr method from within a class method, you would pass self as the first argument.
main.py
class GenericClass(): def __init__(self, dictionary): for key, value in dictionary.items(): setattr(self, key, value) my_dict = {'name': 'bobbyhadz', 'age': 30} obj1 = GenericClass(my_dict) print(getattr(obj1, 'name')) # ๐Ÿ‘‰๏ธ bobbyhadz print(getattr(obj1, 'age')) # ๐Ÿ‘‰๏ธ 30

You can also use the SimpleNamespace class if you need to create a generic object to which you can add attributes.

# Add attributes to an Object using SimpleNamespace()

This is a two-step process:

  1. Use the SimpleNamespace class to create an object.
  2. Use the setattr() function to add attributes to the object.
main.py
from types import SimpleNamespace obj1 = SimpleNamespace() setattr(obj1, 'salary', 100) setattr(obj1, 'language', 'Python') print(getattr(obj1, 'salary')) # ๐Ÿ‘‰๏ธ 100 print(getattr(obj1, 'language')) # ๐Ÿ‘‰๏ธ Python print(obj1) # ๐Ÿ‘‰๏ธ namespace(salary=100, language='Python')

add attributes to object using simple namespace

The code for this article is available on GitHub

The SimpleNamespace class is a subclass of object and provides attribute access to its namespace.

We can't add attributes to instances of the built-in object class, however, we can attributes to instances of the SimpleNamespace class.

The class can also be initialized with keyword arguments.

main.py
from types import SimpleNamespace obj1 = SimpleNamespace(name='bobby', age=30) setattr(obj1, 'salary', 100) setattr(obj1, 'language', 'Python') print(getattr(obj1, 'salary')) # ๐Ÿ‘‰๏ธ 100 print(getattr(obj1, 'language')) # ๐Ÿ‘‰๏ธ Python print(getattr(obj1, 'name')) # ๐Ÿ‘‰๏ธ bobby print(getattr(obj1, 'age')) # ๐Ÿ‘‰๏ธ 30 # ๐Ÿ‘‡๏ธ namespace(name='bobby', age=30, salary=100, language='Python') print(obj1)

The SimpleNamespace class is quite convenient when you need to create a blank object to which you can add attributes.

Attributes cannot be added to the built-in object class, so the SimpleNamespace should be your preferred approach.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev