Python setattr() Function – Tutorial with Examples

The setattr() function in Python is a built-in function that sets the value of an attribute of an object. The setattr() function takes an object and an attribute name, and sets the value of the attribute to the specified value. The attribute name is passed as a string, and the value can be of any type.

Syntax

setattr(object, name, value)

Parameters

  • object : The object for which the attribute is to be set.
  • name : The name of the attribute, passed as a string.
  • value : The value of the attribute to be set.

Return Value

The setattr() function does not return any value. It simply sets the value of the specified attribute of the specified object.

Examples

Example 1: Setting an Attribute of a Class

# Setting an attribute of a class
class Person:
    pass

person = Person()
setattr(person, 'name', 'John Doe')
print(person.name)

Output

John Doe

In this example, a new class named “Person” is created. An instance of the class is then created, and the setattr() function is used to set the value of the “name” attribute of the instance to “John Doe”. The value of the “name” attribute can then be accessed using the dot (.) operator, and is printed to the console.

Example 2: Setting an Attribute of a Dictionary

# Setting an attribute of a dictionary
person = {'name': 'Jane Doe'}
setattr(person, 'age', 30)
print(person)

Output

{'name': 'Jane Doe', 'age': 30}

In this example, a dictionary named “person” is created, and the setattr() function is used to set the value of the “age” attribute of the dictionary to 30. The dictionary can then be printed, and it is seen that the “age” attribute has been successfully added to the dictionary.

Example 3: Setting an Attribute of an Object

# Setting an attribute of an object
class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

car = Car('Toyota', 'Camry')
setattr(car, 'year', 2020)
print(car.year)

Output

2020

In this example, a class named “Car” is created. An instance of the class is then created and passed the “make” and “model” attributes. The setattr() function is then used to set the value of the “year” attribute of the “car” object to 2020. The value of the “year” attribute can then be accessed using the dot (.) operator, and is printed to the console.

Use Cases

The setattr() function is commonly used in the following scenarios:

  • When you want to add an attribute to an object dynamically, without having to hard-code it into the object’s definition.
  • When you want to set the value of an attribute that may or may not exist, without having to check if it exists first.
  • When you want to set the value of an attribute that is not part of an object’s definition, but should be treated as an attribute nonetheless.

The setattr() function is a useful tool for making an object more flexible and dynamic, allowing you to add and set attributes as needed, without having to modify the object’s definition.

It should be noted that the setattr() function should be used with caution, as it can lead to unexpected behavior if used improperly. For example, setting an attribute that is already defined with a different type or value can lead to unexpected results. It is recommended to use the setattr() function only when you have a clear understanding of the behavior and consequences of doing so.

Leave a Reply

Your email address will not be published. Required fields are marked *