On 23 October 2013 08:58, Albert-Jan Roskam <fo...@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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to