[Python-ideas] Computed attribute names (Re: Assignment decorators)

Steven D'Aprano steve at pearwood.info
Fri Mar 4 06:09:24 CET 2011

Greg Ewing wrote:
> MRAB wrote:
>> Or:
>>     x = a.(name)
> That's what I'd go for as well, if I were to go for
> something like this at all.
> FWIW, there is one thing you could do with this that
> is currently rather painful:
>    x.(name) += 1
> Doing that with getattr/setattr currently requires
> about 3 lines of code.

Is there a global newline shortage I haven't heard about? *wink*

In any case:

setattr(x, name, getattr(x, name, 0)+1)

looks like one line to me. If you don't like the DRY violation, the 
obvious helper function makes it easy to write that as (say)

incattr(x, name, 1)


More information about the Python-ideas mailing list