[Python-ideas] Shorthand syntax for get/set/delattr (was Re: Dict-like object with property access)

Nick Coghlan ncoghlan at gmail.com
Sun Feb 5 01:38:40 CET 2012

On Sun, Feb 5, 2012 at 10:20 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> It would be a *lot* cleaner if we could just use a normal assignment
> statement instead of builtin functions to perform the name binding. As
> it turns out, for ordinary instances, we can already do exactly that:
>    for attr in "attr1 attr2 attr3".split():
>        vars(x)[attr] = vars(y)[attr]

That can obviously also be written:

    xa, ya = vars(x), vars(y)
    for attr in "attr1 attr2 attr3".split():
        va[attr] = ya[attr]

In other words, don't think about new syntax. Think about how to
correctly implement a full object proxy that provides the
MutableMapping interface, with get/set/delitem on the proxy
corresponding with get/set/delattr on the underlying object. Then
think about whether or not returning such an object from vars() would
be backwards compatible, or whether a new API would be needed to
create one (e.g. attrview(x)).

Finally, such an object can be prototyped quite happily outside the
standard library, so consider writing it and publishing it on PyPI as
a standalone module.


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

More information about the Python-ideas mailing list