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

Rustom Mody rustompmody at gmail.com
Sun Feb 1 22:52:39 EST 2015


On Sunday, February 1, 2015 at 9:51:11 PM UTC+5:30, Steven D'Aprano wrote:
> Rustom Mody 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...
> 
> Surely the right answer would have been "I don't know, let's check the
> interactive interpreter first, and the docs second."
> 
> Checking the REPL first would have revealed that [].__dir__ raises
> AttributeError. In other words, lists don't have a __dir__ method.

??

$ python3
Python 3.4.2 (default, Oct  8 2014, 13:08:17) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> [].__dir__
<built-in method __dir__ of list object at 0x7f5dd2209f48>
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> [].__dir__()
['__rmul__', '__str__', '__gt__', 'remove', '__class__', '__getitem__', '__format__', '__ne__', '__sizeof__', '__contains__', '__reduce__', '__add__', 'sort', 'count', 'extend', '__mul__', '__imul__', '__reduce_ex__', '__setitem__', '__doc__', '__ge__', 'copy', '__init__', '__iadd__', '__hash__', '__delitem__', 'insert', '__iter__', '__repr__', '__le__', '__setattr__', 'reverse', '__new__', '__eq__', '__len__', 'index', '__lt__', 'clear', '__subclasshook__', 'append', '__dir__', '__reversed__', '__getattribute__', 'pop', '__delattr__']
>>> 

What you are describing seems to be true for python2 though



More information about the Python-list mailing list