[Python-ideas] @setattr(obj, [name])

George Sakkis george.sakkis at gmail.com
Sun Jun 6 22:26:07 CEST 2010


On Sun, Jun 6, 2010 at 6:05 PM, Andrey Fedorov <anfedorov at gmail.com> wrote:

> George Sakkis wrote:
>>
>> Thoughts ?
>
> I liked the idea, then realized that I was misunderstanding it deeply. I
> would have expected
>
> @setattr('key', "value")
> def bar(): pass
>
> make
>
> bar.key == "value"

Yeah that would be a useful decorator too; I'd actually use **kwargs
so that you could do multiple bindings at once with
@setattr(key1=value1, key2=value2, ...).

> This was just my intuition from hearing "setattr decorator" and glancing
> over your email. But deceiving name aside, I think this is a useful
> decorator. I would just call it @method_of.

I agree that overloading setattr() would be a bad idea given the two
(at least) different interpretations. Still "method_of" is not quite
right either since it can also be used as a class decorator; moreover
the 'obj' argument does not have to be a class, it can be a plain
instance.

George



More information about the Python-ideas mailing list