[Python-ideas] Add a dict with the attribute access capability

Nick Coghlan ncoghlan at gmail.com
Fri Dec 1 01:08:28 EST 2017


On 30 November 2017 at 05:11, Barry Warsaw <barry at python.org> wrote:
> Serhiy Storchaka wrote:
>> In 3.7 I have removed an old-deprecated plistlib.Dict. [1] Actually it
>> already was deprecated when the plistlib module was added to the regular
>> stdlib in Python 2.6.
>>
>> Raymond noticed that that capability seemed nice to have.
>
> So nice in fact that I'm sure I've reimplemented something similar
> several times. :)

Note that we do offer a simple namespace type:

    >>> from types import SimpleNamespace as ns
    >>> data = ns(a=1, b=2, c=3)
    >>> data.a
    1
    >>> vars(data)["a"]
    1
    >>> vars(data)["a"] = 3
    >>> data.a
    3

It was added as part of adding sys.implementation, since it's the
storage type used for that:

    >>> import sys
    >>> type(sys.implementation)
    <class 'types.SimpleNamespace'>

So the only thing we don't currently offer is a type that provides
both attribute access and mapping access on the *same* object - for
SimpleNamespace we require that you request the mapping API with
"vars(ns)".

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list