Last updated: Apr 10, 2024
Reading timeยท3 min
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.
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
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:
Name | Description |
---|---|
object | the object to which the attribute is added |
name | the name of the attribute |
value | the 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.
class GenericClass(): pass obj1 = GenericClass() setattr(obj1, 'salary', 100) setattr(obj1, 'age', 30) print(getattr(obj1, 'salary')) # ๐๏ธ 100 print(getattr(obj1, 'age')) # ๐๏ธ 30
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.
object
class doesn't have a __dict__
attribute, therefore we can't assign attributes to an instance of the class.You can also use dot notation to add an attribute to an object.
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
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.
You can use a for loop if you need to add multiple attributes to an object.
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
We used a for
loop to iterate over a dictionary's items and added the
key-value pairs as attributes to the object.
setattr
method from within a class method, you would pass self
as the first argument.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.
This is a two-step process:
SimpleNamespace
class to create an object.setattr()
function to add attributes to the object.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')
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.
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.
You can learn more about the related topics by checking out the following tutorials: