Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95257

Re: looking for standard/builtin dict-like data object

References <CAPTjJmoLSZjoerPeJ5a+Nh1ftcQfqgdcF-xB=3Xn7m2WKxyjHA@mail.gmail.com> <20150811042807.GA56342@cskk.homeip.net> <CAK_ErkhUGviODjHsptmb6L4GXNkBb352rXTYTVvj0MyD4dTwgg@mail.gmail.com>
Date 2015-08-11 22:11 +1000
Subject Re: looking for standard/builtin dict-like data object
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.79.1439295076.3627.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Aug 11, 2015 at 10:06 PM, Vladimir Ignatov <kmisoft@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: looking for standard/builtin dict-like data object Chris Angelico <rosuav@gmail.com> - 2015-08-11 22:11 +1000

csiph-web