Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85753

What the Pythons docs means by "container" ?

Newsgroups comp.lang.python
Date 2015-02-17 13:21 -0800
Message-ID <71e8463f-2a60-4fb0-a5b7-0ca7cd3efece@googlegroups.com> (permalink)
Subject What the Pythons docs means by "container" ?
From candide <c.candide@laposte.net>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

What the Pythons docs means by "container" ? candide <c.candide@laposte.net> - 2015-02-17 13:21 -0800
  Re: What the Pythons docs means by "container" ? Cameron Simpson <cs@zip.com.au> - 2015-02-18 08:39 +1100
    Re: What the Pythons docs means by "container" ? candide <c.candide@laposte.net> - 2015-02-17 16:30 -0800
      Re: What the Pythons docs means by "container" ? Chris Angelico <rosuav@gmail.com> - 2015-02-18 11:49 +1100
        Re: What the Pythons docs means by "container" ? candide <pascal.ortiz@gmail.com> - 2015-02-17 18:14 -0800
          Re: What the Pythons docs means by "container" ? MRAB <python@mrabarnett.plus.com> - 2015-02-18 18:04 +0000
          Re: What the Pythons docs means by "container" ? Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-18 11:50 -0700
          Re: What the Pythons docs means by "container" ? Ethan Furman <ethan@stoneleaf.us> - 2015-02-18 11:43 -0800
          Re: What the Pythons docs means by "container" ? Terry Reedy <tjreedy@udel.edu> - 2015-02-18 15:04 -0500
            Re: What the Pythons docs means by "container" ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-19 10:03 +1100
              Re: What the Pythons docs means by "container" ? Ben Finney <ben+python@benfinney.id.au> - 2015-02-19 11:05 +1100
              Re: What the Pythons docs means by "container" ? Chris Angelico <rosuav@gmail.com> - 2015-02-19 11:33 +1100
  Re: What the Pythons docs means by "container" ? Terry Reedy <tjreedy@udel.edu> - 2015-02-17 19:54 -0500
  Re: What the Pythons docs means by "container" ? perfectican <perfectican@gmail.com> - 2015-02-19 02:59 -0800
    Re: What the Pythons docs means by "container" ? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-19 11:20 +0000
  Re: What the Pythons docs means by "container" ? Rustom Mody <rustompmody@gmail.com> - 2015-02-20 03:37 -0800
    Re: What the Pythons docs means by "container" ? Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-20 11:38 -0700

csiph-web