自定义 __dir__() returns 按字母顺序排序的属性列表

custom __dir__() returns list of attributes sorted alphabetically

我在我的基础 class 中有一个自定义 __dir__ 实现,它应该是 return 所有用户定义的 __slots__ 属性的列表。一般来说,这是有效的,但它似乎在 returns 之前对结果执行 sort,即使我没有编程这样做(我需要属性以完全相同他们被分配的顺序)。

一个例子:

class A:
    __slots__ = ['b', 'a']

    def __dir__(self):
        slot_attrs = []
        for parent_class in reversed(type(self).__mro__[:-1]):
            for attr in parent_class.__slots__:
                slot_attrs.append(attr)
        for attr in self.__slots__:
            slot_attrs.append(attr)
        return slot_attrs


class B(A):
    __slots__ = ['c', 'd']
    pass


class C(B):
    __slots__ = []
    pass


class D:
    __slots__ = ['b', 'a']

    def slots(self):
        slot_attrs = []
        for parent_class in reversed(type(self).__mro__[:-1]):
            for attr in parent_class.__slots__:
                slot_attrs.append(attr)
        for attr in self.__slots__:
            slot_attrs.append(attr)
        return slot_attrs


class E(D):
    __slots__ = ['c', 'd']
    pass


class F(E):
    pass

slots()__dir__() 的输出在我看来应该是相同的。

但是,这发生了:

>>>c = C()
>>>f = F()

>>>print(dir(c))
['a', 'b', 'c', 'd']
>>>print(f.slots())
['b', 'a', 'c', 'd', 'c', 'd', 'c', 'd']

我可以有点理解它按字母顺序对输出进行排序,当使用dir()时——这是documented in the docs。但是,它看起来像一个错误 - 或者至少对我来说是意外的行为 - 即使我已经定义了自定义 __dir__ 方法,它也会对输出进行排序。

第二个输出让我完全无法玩游戏。它表明 dir 也使用某种过滤器,可能是 set 来避免重复输出,因为代码是相同的但调用 slots() returns 重复值。

我既不 A) 首先理解它为什么这样做,也不理解 B) dir 到底在做什么。

这里有任何指示吗?

编辑:
第二种情况已解决 - __mro__ 包含调用者的 class,以及它继承自的所有 classes - 因此 class 被包含两次。 即:

>>>F.__mro__
(<class '__main__.F'>, <class '__main__.E'>, <class '__main__.D'>, <class 'object'>)

编辑 2:
情节变厚了。评论中提到的问题更清楚地说明了这种行为的来源:

>>Couldn't __dir__ also be allowed to return a tuple?
no, because tuples are not sortable, and i don't want to 
over complicate the c-side code of PyObject_Dir. 
having __dir__ returning only a list is equivalent to 
__repr__ returning only strings.

这似乎源自 C 源代码,在 __dir__ 实现之前。

编辑 3:
我开了一个issue on python's bug tracker。让我们看看共识是什么。但是,我希望这会被搁置(如果有的话),因为 dir() 是,afaik,主要设计用于在 IDLE 等中进行检查。

根据 issue opened on the Python bug tracker:

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'>