[Chicago] Fun at Pumping Station last Wednesday night.

Daniel Uber djuber at gmail.com
Mon May 25 19:52:52 CEST 2015


Hi Douglas,

About the magic methods, the short answer is that these are functions
that python calls, not you. You might ask why to call next(mygenerator)
instead of mygenerator.__next__.__call__(), also, since these again do
the same thing. Does mygenerator.__next__.__call__.__call__() do the
same thing (yes).

Rafe Kettler had a good writeup on these at
http://rafekettler.com/magicmethods.html or at
http://www.rafekettler.com/magicmethods.pdf


Which would you rather see in code when you were reading? Of course the
'natural' one that's built into the language. The situation with next
and __next__ is completely parallel.

>>> alist = [1,2,3]
>>> alist[0]
1
>>> alist.__getitem__(0)
1
>>> alist.__getitem__.__call__(0)
1
>>> alist.__getitem__.__call__.__call__(0)
1



More information about the Chicago mailing list