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__
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…