What the Pythons docs means by "container" ?

candide c.candide at laposte.net
Tue Feb 17 16:21:17 EST 2015


Official Python documentation very frequently invokes a mysterious *container* data structure. The PLR manual explains :

--------------------------
Some objects contain references to other objects; these are called containers.
--------------------------

So containers contain : what a great definition!

To be more precise, the "container" wording suggests a data structure _storing_ items somewhere and that the number of items in the container has a memory footprint. This is exacly the "container" definition given by the ISO C++ standard (cf.  http://www.open-std.org/jtc1/sc22/open/n2356/lib-containers.html) :

--------------------------
Containers are objects that store other objects.
--------------------------

So I was considering a Python range object NOT being a container: indeed, range(10**6) does NOT store 10**6 integers and range(10**6) has a small memory footprint in the contrary to the associated list :

----------------------------------------------
>>> from sys import getsizeof
>>> r = range(10**6)
>>> L = list(r)
>>> getsizeof(r)
24
>>> getsizeof(L)
4500056
>>> 
----------------------------------------------

Then, mining the official docs, I realized that the collections module provides an ABC Container class allowing you to know if a given object is a container or not. And what a surprise, a range object IS a container !

----------------------------------------------
>>> from collections import Container
>>> r = range(10**6)
>>> isinstance(r, Container)                                                            
True                                                                                    
>>>
----------------------------------------------

So, what documentation means by the generic term of "container"? 

I agree with the existence of built-in containers (list, dict and the like) and the existence of special containers provided by the collections module (most of them inheriting from a built-in container) but I can't find a precise definition explaining what is a "container object" or a "container type". 

The docs at :

https://docs.python.org/3.2/reference/datamodel.html#object.__contains__

explains that a container is an object compatible with the membership test (in and not in operators). So a file is a container ? recall a file supports the in operator :

----------------------------------------------
$ touch my_test.txt
$ python3.2
Python 3.2.3 (default, Feb 28 2014, 00:22:33) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 42 in open("my_test.txt")
False
>>> from collections import Container
>>> isinstance(open("my_test.txt"), Container)
False
>>> 
----------------------------------------------




Reference of interest :

http://blog.wachowicz.eu/?p=132
http://stackoverflow.com/questions/11575925/what-exactly-are-containers-in-python-and-what-are-all-the-python-container




More information about the Python-list mailing list