python有几种容器_什么是“容器”?在python中? (以及所有的python容器类型是什么?)...

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

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.

Edit

For Python 2.7.3:

Checked builtin types which are containers:

(isinstance(object, collections.Container) returns True)

Containers which have a __contains__ method defined:

All builtin sequence types: Lists, bytearrays, strings, unicode strings and

tuples.

Dictionaries

All builtin set types: sets and frozensets

Containers which do not have a __contains__ method defined:

xrange objects

Checked builtin types which are not containers:

(isinstance(object, collections.Container) returns False):

Int objects

Float objects

Long objects

Boolean objects

Module objects

File objects

Buffer objects

The None object

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.

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

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).

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

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值