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.
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.
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.
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__
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With