Python – Set Object Attribute Value – setattr() Function with Examples

setattr() function is used to find the sets the value of given attribute of an object in Python for the given input.

setattr() function is a part of Python Built-in Functions.

Syntax of setattr() Function

The syntax of setattr() function in Python is:

setattr(object, name, value)

 

Parameters of setattr() Function in Python

x – Where x is any valid . This parameter is required.

 

Compatibility

setattr() function is available and compatible with both Python 2.x and 3.x.

 

Return Value of setattr() Function in Python

setattr() function returns sets the value of given attribute of an object for the given input.

 

Python setattr() Function Example 1

Example CODE

Output:

 

Python setattr() Function Example 2

Example CODE

Output: