什么是“容器”?在python中? (以及所有的python容器类型是什么?) [英] What exactly are "containers" in python? (And what are all the python container types?)

查看:574
本文介绍了什么是“容器”?在python中? (以及所有的python容器类型是什么?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python文档经常提到容器。 例如

The python documentation frequently speaks of "containers". E.g. :


如果check_circular为False(默认值:True),则将跳过容器类型的循环
参考检查,而循环
引用将导致OverflowError(或更糟)。

If check_circular is False (default: True), then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse).

但是我找不到容器的任何正式定义,

But I can't find any official definition of containers, neither a list of them.

对于Python 2.7.3:

For Python 2.7.3:

已检查的内置类型是容器:

isinstance(object,collections.Container) 返回 True


  1. 包含以下内容的容器定义了 __ contains __ 方法:


  • 所有内置序列类型:列表,字节数组,字符串,unicode字符串和
    元组。

  • 字典

  • 所有内置集合类型:集和冻结集

没有 __ contains __ 的容器定义的方法:

Containers which do not have a __contains__ method defined:


  • xrange对象

已检查不是容器的内置类型:

isinstance (对象,collections.Container)返回 False ):


  • 整数对象

  • 浮动对象

  • 长对象

  • 布尔对象

  • 模块对象

  • 文件对象

  • 缓冲对象

  • 无对象

  • Int objects
  • Float objects
  • Long objects
  • Boolean objects
  • Module objects
  • File objects
  • Buffer objects
  • The None object

告诉我您检查了 isinstance(object,collections.Container),然后将它们添加到列表中。

Tell me which other builtin types you have checked for isinstance(object, collections.Container) and I'll add them to the list.

推荐答案

容器是包含任意数量其他对象的任何对象。通常,容器提供一种访问所包含的对象并对其进行迭代的方法。

Containers are any object that holds an arbitrary number of other objects. Generally, containers provide a way to access the contained objects and to iterate over them.

容器的示例包括 tuple 列表集合 dict ;这些是内置容器。在 集合 模块。

Examples of containers include tuple, list, set, dict; these are the built-in containers. More container types are available in the collections module.

严格来说, collections.abc.Container 抽象基类( collections.Container 在所有类型中均适用通过 __ contains __ 魔术方法支持 in 运算符;因此,如果您可以在y中写入 x ,那么 y 通常是 容器,但不是总是:容器和一般的 iterables 之间的重要区别是,当迭代时,容器将返回它们拥有引用的现有对象,而生成器和例如 file 对象每次都会创建一个新对象。这对垃圾回收和深度对象遍历有影响(例如 deepcopy 和序列化)。

Strictly speaking, the collections.abc.Container abstract base class (collections.Container in Python2) holds for any type that supports the in operator via the __contains__ magic method; so if you can write x in y then y is usually a container, but not always: an important point of difference between containers and general iterables is that when iterated over, containers will return existing objects that they hold a reference to, while generators and e.g. file objects will create a new object each time. This has implications for garbage collection and deep object traversal (e.g. deepcopy and serialisation).

例如, iter(lambda:random.choice(range(6)),0)支持 in 运算符,但这肯定是不是一个容器!

As an example, iter(lambda: random.choice(range(6)), 0) supports the in operator, but it is certainly not a container!

Collections.abc.Container 抽象库的意图该类仅考虑 __ contains __ 魔术方法,而没有其他支持 in 运算符的方法是,应该使用真正的容器能够在一次操作中测试收容性,而不会明显改变内部状态。由于 Collections.abc.Container __ contains __ 定义为抽象方法,因此可以保证 isinstance(x,collections.abc.Container)然后 x 支持 in 运算符。

The intent of the Collections.abc.Container abstract base class in only considering the __contains__ magic method and not other ways of supporting the in operator is that a true container should be able to test for containment in a single operation and without observably changing internal state. Since Collections.abc.Container defines __contains__ as an abstract method, you are guaranteed that if isinstance(x, collections.abc.Container) then x supports the in operator.

实际上,所有容器都将具有 __ contains __ 魔术方法。但是,在测试对象是否为容器时,应使用 isinstance(x,collections.abc.Container)来明确和向前兼容,容器子类检查已更改。

In practice, then, all containers will have the __contains__ magic method. However, when testing whether an object is a container you should use isinstance(x, collections.abc.Container) for clarity and for forward compatibility should the Container subclass check ever be changed.

这篇关于什么是“容器”?在python中? (以及所有的python容器类型是什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆