Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85754
| Date | 2015-02-18 08:39 +1100 |
|---|---|
| From | Cameron Simpson <cs@zip.com.au> |
| Subject | Re: What the Pythons docs means by "container" ? |
| References | <71e8463f-2a60-4fb0-a5b7-0ca7cd3efece@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18794.1424209210.18130.python-list@python.org> (permalink) |
On 17Feb2015 13:21, candide <c.candide@laposte.net> 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. >-------------------------- And this is the basic thing, as you understand. >So containers contain : what a great definition! Shrug. It is a useful descriptive term when you want to talk about things. You might keep in mind that a "container"'s _primary_ purpose is usually its containing function. So a list. But not so some arbitrary object, even though most objects have references to 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 [...] >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 ! You might find it duck types like other containers. [...] >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). Duck typing again: this bases the term on behaviour, not memory use. >So a file is a container ? recall a file supports the in operator : Yes and no. A file can be iterated. "in" on a file reads the file as a side effect of iterating over it. A generator also supports "in", again by iteration. Neither of these things has a .__contains__ method; the "in" operator falls back to iteration if there is no .__contains__. A container usually lets you test "in" in a nondestructive/nonconsuming fashion. An iterable can be "in"ed, but it will be consumed. So iterables are not, of themselves, containers. So, range? A range object knows its bounds and state; it can answer "in" without consuming the range iteration, so it is effectively a container. Hoping this helps. Cheers, Cameron Simpson <cs@zip.com.au> [Alain] had been looking at his dashboard, and had not seen me, so I ran into him. - Jean Alesi on his qualifying prang at Imola '93
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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