I've been playing around with Python recently, and one thing I'm finding a bit odd is the extensive use of 'magic methods', e.g. to make its length available, an object implements a method, def __len__(self)
, and then it is called when you write len(obj)
.
I was just wondering why objects don't simply define a len(self)
method and have it called directly as a member of the object, e.g. obj.len()
? I'm sure there must be good reasons for Python doing it the way it does, but as a newbie I haven't worked out what they are yet.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…