[Python-ideas] a simple namespace type

Eric Snow ericsnowcurrently at gmail.com
Tue May 22 18:26:20 CEST 2012


Below I've included a pure Python implementation of a type that I wish
was a builtin.  I know others have considered similar classes in the
past without any resulting change to Python, but I'd like to consider
it afresh[1][2].

  class SimpleNamespace:
      """A simple attribute-based namespace."""
      def __init__(self, **kwargs):
          self.__dict__.update(kwargs)  # or self.__dict__ = kwargs
      def __repr__(self):
          keys = sorted(k for k in self.__dict__ if not k.startswith('_'))
          content = ("{}={!r}".format(k, self.__dict__[k]) for k, v in keys)
          return "{}({})".format(type(self).__name__, ", ".join(content))

This is the sort of class that people implement all the time.  There's
even a similar one in the argparse module, which inspired the second
class below[3].  If the builtin object type were dict-based rather
than slot based then this sort of namespace type would be mostly
superfluous.  However, I also understand how that would add an
unnecessary resource burden on _all_ objects.  So why not a new type?

Nick Coghlan had this objection recently to a similar proposal[4]:

    Please, no. No new
    just-like-a-namedtuple-except-you-can't-iterate-over-it type, and
    definitely not one exposed in the collections module.

    We've been over this before: collections.namedtuple *is* the standard
    library's answer for structured records. TOOWTDI, and the way we have
    already chosen includes iterability as one of its expected properties.

As you can see he's referring to "structured records", but I expect
that his objections could be extended somewhat to this proposal.  I
see where he's coming from and agree relative to structured records.
However, I also think that a simple namespace type would be a benefit
to different use cases, namely where you want a simple dynamic
namespace.

Making a simple namespace class is trivial and likely just about
everyone has written one:  "class Namespace: pass" or even
"type('Namespace', (), {})".  Obviously the type in this proposal has
more meat, but that's certainly not necessary.  So why a new type?

The main reason is that as a builtin type the simple namespace type
could be used in builtin modules[5][6][7].

Thoughts?

-eric


[1] http://mail.python.org/pipermail/python-dev/2012-May/119387.html
[2] http://mail.python.org/pipermail/python-dev/2012-May/119393.html
[3] http://hg.python.org/cpython/file/dff6c506c2f1/Lib/argparse.py#l1177
[4] http://mail.python.org/pipermail/python-dev/2012-May/119412.html
[5] http://mail.python.org/pipermail/python-dev/2012-May/119395.html
[6] http://mail.python.org/pipermail/python-dev/2012-May/119399.html
[7] http://mail.python.org/pipermail/python-dev/2012-May/119402.html

--------------------------

class Namespace(SimpleNamespace):
    def __dir__(self):
        return sorted(k for k in self.__dict__ if not k.startswith('_'))
    def __eq__(self, other):
        return self.__dict__ == other.__dict__
    def __ne__(self, other):
        return self.__dict__ != other.__dict__
    def __contains__(self, name):
        return name in self.__dict__



More information about the Python-ideas mailing list