This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author christian.heimes
Recipients christian.heimes, nlsdfnbch
Date 2017-10-19.08:35:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1508402111.79.0.213398074469.issue31816@psf.upfronthosting.co.za>
In-reply-to
Content
https://docs.python.org/3/library/functions.html#dir also states that "The resulting list is sorted alphabetically." The section has an example where __dir__ returns an unsorted list but dir() returns a sorted list:

>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']

Since the primary purpose of dir() is convenient use for humans, sorting makes perfectly sense. If you need tight control over order of values, you should make your object iterable instead or provide another method.

Several dunder methods perform some sort of post-processing or post-check:

>>> class Example:
...     def __bool__(self): return 2
... 
>>> bool(Example())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __bool__ should return bool, returned int

>>> class MyInt(int):
...     pass
... 
>>> type(MyInt(1))
<class '__main__.MyInt'>
>>> class Example:
...     def __int__(self):
...         return MyInt(1)
... 
>>> int(Example())
1
>>> type(int(Example()))
<class 'int'>
History
Date User Action Args
2017-10-19 08:35:11christian.heimessetrecipients: + christian.heimes, nlsdfnbch
2017-10-19 08:35:11christian.heimessetmessageid: <1508402111.79.0.213398074469.issue31816@psf.upfronthosting.co.za>
2017-10-19 08:35:11christian.heimeslinkissue31816 messages
2017-10-19 08:35:11christian.heimescreate