[Tutor] Beginner Question
Oscar Benjamin
oscar.j.benjamin at gmail.com
Wed Oct 23 11:20:56 CEST 2013
On 23 October 2013 08:58, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> So the built-in 'len()' is *really* a function, but calls to len() implemented by __len__ are method calls *disguised* as function calls? I sometimes find it easier to write calls to special methods the "normal" way, e.g. instead of "x + y" just write it like "x.__add__(y)" This makes special methods more like other methods and therefore easier to understand, to me at least.
Please don't do that. Firstly it looks horrible. Secondly they're not
equivalent. The equivalent of x + y is operator.add(x, y) but don't
use that either. It's not easier to understand and it's less
efficient.
When you wrate a+b the interpreter actually calls a bunch of different
methods: a.__add__(b), b.__radd__(a), a.__coerce__(b) etc. I don't
know the full sequence and it's not consistent across Python
implementations. Eryksun recently posted a good example showing how a
== b is handled:
https://mail.python.org/pipermail/tutor/2013-July/097110.html
Oscar
More information about the Tutor
mailing list