python - 除了 dict、list、set 和 tuple 之外,还有其他内置容器吗?

标签 python

dict, list, set, and tuple are Python’s built-in containers.

除了上面还有其他内置的容器吗?

术语“内置容器”来自the doc .

最佳答案

从技术上讲,Python 容器是任何实现 __contains__ 方法 ( source ) 的东西。因此,如果我们只考虑内置函数,而不考虑标准库,我们可以通过这一行得到答案:

>>> [i for i in dir(__builtins__) if hasattr(eval(i), '__contains__')]
['_', '__name__', 'bytearray', 'bytes', 'dict', 'frozenset', 'list', 'range', 'set', 'str', 'tuple']

我们要排除 ___name__ 所以完整的列表是:

bytearraybytesdictfrozensetlistrangesetstrtuple


或者,

>>> [k for k, v in vars(__builtins__).items() if hasattr(v, '__contains__')]
['__name__', '__doc__', '__package__', 'bytearray', 'bytes', 'dict', 'frozenset', 'list', 'range', 'set', 'str', 'tuple', '_']

并且删除 __name____doc____package___ 将得到相同的结果。

关于python - 除了 dict、list、set 和 tuple 之外,还有其他内置容器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58721442/

相关文章:

python - 使用 Pandas 的盈亏

python - 线程 : ValueError: signal number 32 out of range 中的 Pytorch 异常

fmtp 的 Python 正则表达式

java - 是否有与 Python 的 defaultdict 等效的 Java?

python - Python 2.4 中的 SHA256 哈希

python - BeautifulSoup 为 .find 和 .find_all 提供不同的结果

python - Azure 事件中心如何获取最新消息

python - 配置解析器.NoSectionError :No section :'XXX'

python unittest导入类子目录

python - Python 脚本的参数数量可变