dunder-docs (was Python is DOOMED! Again!)

Chris Angelico rosuav at gmail.com
Sun Feb 1 15:45:55 EST 2015


On Sun, Feb 1, 2015 at 4:36 PM, Rustom Mody <rustompmody at gmail.com> wrote:
> The other day I was taking a class in which I was showing
> - introspection for discovering -- help, type, dir etc at the repl
> - mapping of surface syntax to internals eg. a + b ←→ a.__add__(b)
>
> And a student asked me the diff between
> dir([])
> and
> [].__dir__()
>
> I didnt know what to say...

Simple answer: You write dunder methods and the interpreter calls
them. You don't call them yourself.

I can't currently think of any situation where it's appropriate to
call a dunder method manually (cue the swamping of such situations on
the list); you just call dir() or the + operator or whatever it be.

ChrisA



More information about the Python-list mailing list