What the Pythons docs means by "container" ?

Rustom Mody rustompmody at gmail.com
Fri Feb 20 06:37:25 EST 2015


On Wednesday, February 18, 2015 at 2:51:34 AM UTC+5:30, candide wrote:
> 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
> >>> 
> ----------------------------------------------

+1

See https://www.python.org/dev/peps/pep-0246/

There Guido says "something much better is about to happen"

Best as I know its not happened yet...

[Personal note: I am currently trying to teach python
and the lack of a standardized place for all protocols (in python's duck typing sense)
is quite a slow-down-er for me and my class
]



More information about the Python-list mailing list