Why does python use 'magic methods'?
Posted
by Greg Beech
on Stack Overflow
See other posts from Stack Overflow
or by Greg Beech
Published on 2010-04-17T07:26:24Z
Indexed on
2010/04/17
7:33 UTC
Read the original article
Hit count: 437
python
|magic-methods
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.
© Stack Overflow or respective owner