What is difference in getattr() and setattr() functions in Python?


The getattr() method

The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.

Syntax

The syntax of getattr() method is −

getattr(object, name[, default])

The getattr() method can take multiple parameters −

The getattr() method returns −

value of the named attribute of the given object

default, if no named attribute is found

AttributeError exception, if named attribute is not found and default is not defined

The setattr() method

The setattr() method sets the value of given attribute of an object.

Syntax

The syntax of setattr() method is −

setattr(object, name, value)

The setattr() method takes three parameters −

The setattr() method returns None.

Updated on: 20-Feb-2020

305 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements