[Python-ideas] a simple namespace type

Yuval Greenfield ubershmekel at gmail.com
Thu May 24 13:59:15 CEST 2012

On Tue, May 22, 2012 at 7:26 PM, Eric Snow <ericsnowcurrently at gmail.com>wrote:

> 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.
> [...]

I've implemented this a few times as well. I called it "AttributeDict" or

I think adding an __iter__ method would be beneficial. E.g.

class  SimpleNamespace :
     def __init__(self, **kwargs):
         self.__dict__.update(kwargs)  # or self.__dict__ = kwargs
         self.__iter__ = lambda: iter(kwargs.keys())

Why do we need this imo:

* sometimes x.something feels better than x['something']
* to ease duck-typing, making mocks, etc.
* Named tuple feels clunky for certain dynamic cases (why do I need to
create the type for a one-off?)

I wonder if SimpleNameSpace should allow __getitem__ as well...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120524/76924661/attachment.html>

More information about the Python-ideas mailing list