built in - Implementing `__dir__` in Python: Does it need to return a list and does the list need to be sorted? -


i have class implements __dir__ method. however, not entirely nitty gritty details of dir api.

a: required __dir__ returns list? implementation using set avoid listing attributes twice, need convert list before returning? documentation guess has list:

if object has method named dir(), method called , must return list of attributes.

however, not returning list break functionality @ point?


b: result need sorted? documentation bit ambiguous here:

the resulting list sorted alphabetically.

does mean, calling built-in dir automatically sorts data returned __dir__ or mean dir expects sorted data __dir__?

edit: btw, question encompasses python 2 (2.6 , 2.7) , 3 (3.3, 3.4).

under python 2.7.3, answers are:

a: yes, must list:

>>> class f: ...     def __dir__(self): ...             return set(['1']) ...  >>> dir(f()) traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: __dir__() must return list, not set 

b: no, dir sort it.

>>> class g: ...     def __dir__(self): ...             return ['c','b','a'] ...  >>> dir(g()) ['a', 'b', 'c'] 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -