- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的基类中有一个自定义的 __dir__
实现,它应该返回所有用户定义的 __slots__
属性的列表。一般来说,这是可行的,但它似乎在结果返回之前对结果进行了 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()
返回重复值。
我既不 A) 首先理解它为什么这样做,也不理解 B) dir
究竟在做什么。
这里有什么建议吗?
编辑:
第二种情况已解决 - __mro__
包含调用者的类,以及它继承自的所有类 - 因此该类被包含两次。即:
>>>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'>
关于python - 自定义 __dir__() 返回按字母顺序排序的属性列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46824459/
在 MySQL 数据库中,我在表中有一列既有纯数字也有混合数字/字母。没有模式,如果是纯数字我想区分,标记为true,否则为false。有什么好的方法可以使用吗?我试过: ID REGEXP '^[[
这个问题在这里已经有了答案: Numbers as column names of data frames (2 个回答) Why am I getting X. in my column names
尝试提出一个正则表达式来捕获诸如 AB1234 或 BA2321 之类的组。本质上需要捕获以 AB 或 BA 开头并后跟 4 位数字的任何内容。 目前,我有类似的东西,但这似乎没有考虑数字 (AB|B
var z = []; for(var i = 1; i len) z.push("a".repeat(len-i%len)) console.log(z.join("\n")); 关于jav
我需要一个仅用于数字、字母、空格和连字符的正则表达式。 像这样的 ^[a-zA-Z0-9]+$ 得到字母和数字,但我需要一个用于上述。这些真的很难理解! 最佳答案 这是你需要的: /^[0-9A-Za
有没有人可以帮助我解决 PDFBox 中的字母问题我正在尝试打印字母“ń”(波兰语字母)并且我得到了类似 þÿ J 的东西。 Dı B R O W 2S0 :K0 3I. 请帮忙! 最佳答案 我遇到了
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我尽量不让我的文本 block 把一个词分成几个部分并跳到下一行。对于每种设备尺寸,文本都会中断并造成可读性问题。我尝试将 marring-right 与 % 一起使用,但并没有太大帮助。 这是我的哈
这是我第一次向 Stack Overflow 发帖提问。我是编程新手,所以如果我说的奇怪或错误,请原谅。 在下面的文件中;它读取目录并将其保存到变量 nAddress 中。然后删除文件扩展名;将文件分
我希望当用户将鼠标悬停在页面上时,我的页面上的某些文本会重新排列字母。例如,将鼠标悬停在“WORK”上,它就会变成“OWKR”。我怀疑需要 js,但我对 js 还是很陌生。下面是我的 html:
我已经为此工作了几个小时,现在我有点卡住了....请帮助我。我是一个完全的编程障碍。除字母表方法外,所有方法都可以正常工作。 它将接收两个字符(大写或小写)并返回由给定 char 值范围组成的字符串。
我想编写一个程序,在输入的同一行中读取 n 个不同化学元素的名称(其中 1 ≤ n ≤ 17 和 n 也在输入中读取)(名称由空格分开)。化学元素的名称应存储在不同的字符串中以供进一步使用。 由于 n
我想隐藏一个字母,并在链接中显示另一个字母,当然,悬停字母的样式不同。例如: 这是一个... ...normal link. 这是一个... ...hovêrêd lînk. 如何实现?谢谢。 编辑:
我一直被这个相当愚蠢的想法所挑战。 所以我可以用 Blabla[span class=superI]i[/span]rest 替换所有出现的“i”:) 我的想法是在真正的 i“后面”添加一个额外的(红
本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。 ?
下面给大家介绍下JS正则表达式 必须包含数字、字母、特殊字符 js正则表达式要求: 1. 必须包含数字、英文字母、特殊符号且大于等于8位 2. 特殊符号包括: ~!@#$%^&* 正
我在这里和网上四处寻找解决方案。 问题是我只想接受信件。但是,如果我至少输入一个字母,无论是否有符号或数字,它都会接受。如何获得仅 封信? if (!preg_match("/[a-zA-Z]/",
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 去年关闭。 Improve th
制表符分隔的文本文件,实际上是数据库表的导出(使用 bcp),具有以下形式(前 5 列): 102 1 01 e113c 3224.96 12 102 1 01 e185
我需要循环遍历数据数组并为每个数组值打印一个“递增”字母。我知道我可以做到这一点: $array = array(11, 33, 44, 98, 1, 3, 2, 9, 66, 21, 45); //
我是一名优秀的程序员,十分优秀!