Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use __setattr__ in python?

Tags:

People also ask

What is use of Setattr () method in inheritance?

What is the use of the setattr() method in inheritance? The use case of setattr() in inheritance is the same, i.e., to assign value to the attributes of an object.

What is Setattr bracket used for?

The bracket allows the use of different auxiliaries, working with the third central rectangular slot, like peer to peer anchorage, or the hooks for inter maxillary elastics and for closing space.

What is Setattr () and Getattr () used for?

Python setattr() and getattr() goes hand-in-hand. As we have already seen what getattr() does; The setattr() function is used to assign a new value to an object/instance attribute.

What does Setattr return?

setattr() Return Value The setattr() method returns None .


I don't know for why using __setattr__ instead simple referencing like x.a=1.

I understand this example:

class Rectangle:     def __init__(self):         self.width = 0         self.height = 0   x=Rectangle() x.width=20 x.__setattr__('height',30) setattr(x,'width',99) 

but don't get why using code depending on string ('height').

Could you explain me what are advantages of __setattr__ ?