[Python-ideas] Anonymous namedtuples

Steven D'Aprano steve at pearwood.info
Tue Apr 19 12:44:41 EDT 2016


On Tue, Apr 19, 2016 at 10:06:28PM +1000, Steven D'Aprano wrote:
> On Tue, Apr 19, 2016 at 11:25:07AM +0100, Paul Moore wrote:
> 
> > Possibly the docs for namedtuple should refer the user to
> > SimpleNamespace as an alternative if "being a tuple subclass" isn't an
> > important requirement. Or possibly SimpleNamespace should be in the
> > collections module (alongside namedtuple) rather than in the types
> > module?
> 
> +1 
> 
> I'm repeatedly surprised that SimpleNamespace isn't in collections.

I've thought about this some more, and I've decided that I don't think 
it should be in collections. It's not a collection.

Strangely, "collection" is not in the glossary:

https://docs.python.org/dev/glossary.html

(neither is "container") and there doesn't appear to be an ABC for what 
makes a collection, but the docs seem to suggest that "collections" are 
another word for "containers":

Quote:


  8.3. collections — Container datatypes

  Source code: Lib/collections/__init__.py

  This module implements specialized container datatypes providing 
  alternatives to Python’s general purpose built-in containers, dict, 
  list, set, and tuple.


https://docs.python.org/dev/library/collections.html

which mostly matches the old (and I mean really old, going back to 
Python 1.5 days) informal usage of "collection" as "a string, list, 
tuple or dict", that is, a sequence or mapping. (These days, I'd add set 
to the list as well.)

SimpleNamespace is neither a sequence nor a mapping.

We do have a "Container" ABC:

https://docs.python.org/dev/library/collections.abc.html#collections.abc.Container

and SimpleNamespace doesn't match that ABC either.


So I think SimpleNamespace should stay where it is. I guess I'll just 
have to learn that it's a type, not a collection :-)


-- 
Steve


More information about the Python-ideas mailing list