On 01/31/2015 09:36 PM, Rustom Mody wrote:
> 
> And a student asked me the diff between
> dir([])
> and
> [].__dir__()
> 
> I didnt know what to say...
> Now surely the amount of python I dont know is significantly larger than what 
> I know
> Still it would be nice to have surface-syntax ←→ dunder-magic more
> systematically documented

I don't have a complete answer for you, but I can say this:

In simple cases (such as __len__) there is little difference between calling 
the surface operator and the dunder version
(the error message differs in this case).

In more complex cases (such as __add__) using the surface syntax (+) buys lots 
of extras:

  - if one operand is a subclass of the other, calling its __add__ or __radd__ 
method as appropriate
  - if the first operand returns NotImplemented, calling the other operand's 
__radd__ to see if that works
  - possibly others that I don't recall at the moment

Basically, unless you're programming at the system (or class internals) level, 
don't call dunder methods directly.

--
~Ethan~

Attachment: signature.asc
Description: OpenPGP digital signature

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to