looking for standard/builtin dict-like data object

Chris Angelico rosuav at gmail.com
Tue Aug 11 14:11:14 CEST 2015


On Tue, Aug 11, 2015 at 10:06 PM, Vladimir Ignatov <kmisoft at gmail.com> wrote:
>>>> I also thought the stdlib had some kind of "namespace" class with this
>>>> kind
>>>> of API, but I can't find it now:-(
>>>
>>>
>>> It does - types.SimpleNamespace(). It accepts keyword arguments, and
>>> will let you create more attributes on the fly (unlike a namedtuple).
>>
>>
>> Yes, that's it. Thanks!
>>
>
> Ah, sad, sad, sad.  We unfortunately stuck with built-in Python 2.6.x
> in our system.  I see from docs that SimpleNamespace is rather new
> creation (3.3+).  I know 'namedtuple' way, but don't like it as I
> prefer freedom in attribute creation/mutation.  Looks like I have to
> stuck with handmade solution for now.
>

In that case, what I would recommend is: Use your handmade solution,
but call it SimpleNamespace, and make it entirely compatible with the
Python 3.3 one. Then, when you do get a chance to upgrade, all you
need to do is change your import statement, and you're using the
standard library one. Plus, it's going to be easy for anyone else to
read - they'll already know what SimpleNamespace does and what to
expect of it, so they don't have to dig around to see what your class
is doing.

ChrisA


More information about the Python-list mailing list