
It would be nice if setattr() was extended to allow usage as a decorator: class Foo(object): pass @setattr(Foo) def bar(self): print 'bar' @setattr(Foo, 'baz') def get_baz(self): print 'baz'
Here's a pure Python implementation: _setattr = setattr def setattr(obj, *args): if len(args) >= 2: return _setattr(obj, *args) return lambda f: _setattr(obj, args[0] if args else f.__name__, f) or f Thoughts ? George

Am 06.06.2010 14:30, schrieb George Sakkis:
Since this is useful for functions only, I would not try to overload a simple builtin, call it def_on() and put it in my utility module: @def_on(Foo) def method(self): pass Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.

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" 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. - Andrey

On Sun, Jun 6, 2010 at 6:05 PM, Andrey Fedorov <anfedorov@gmail.com> wrote:
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, ...).
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

On Sun, Jun 6, 2010 at 8:30 AM, George Sakkis <george.sakkis@gmail.com> wrote:
Thoughts ?
I'm not sure I understand why you would want to do this. I may just be combative because I hate the over use of decorators. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek

On Sun, Jun 6, 2010 at 6:16 PM, David Stanek <dstanek@dstanek.com> wrote:
It depends on what you mean by "this"; binding a function (or class) to an object as an attribute, or using a decorator to achieve it ? I find a decorator quite elegant in this case, although the use case itself is admittedly much less common in Python than, say, Javascript (with ``obj.property = function(x,y) {...}`` expressions everywhere). George

Am 06.06.2010 14:30, schrieb George Sakkis:
Since this is useful for functions only, I would not try to overload a simple builtin, call it def_on() and put it in my utility module: @def_on(Foo) def method(self): pass Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.

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" 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. - Andrey

On Sun, Jun 6, 2010 at 6:05 PM, Andrey Fedorov <anfedorov@gmail.com> wrote:
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, ...).
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

On Sun, Jun 6, 2010 at 8:30 AM, George Sakkis <george.sakkis@gmail.com> wrote:
Thoughts ?
I'm not sure I understand why you would want to do this. I may just be combative because I hate the over use of decorators. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek

On Sun, Jun 6, 2010 at 6:16 PM, David Stanek <dstanek@dstanek.com> wrote:
It depends on what you mean by "this"; binding a function (or class) to an object as an attribute, or using a decorator to achieve it ? I find a decorator quite elegant in this case, although the use case itself is admittedly much less common in Python than, say, Javascript (with ``obj.property = function(x,y) {...}`` expressions everywhere). George
participants (5)
-
Andrey Fedorov
-
David Stanek
-
Georg Brandl
-
George Sakkis
-
Lie Ryan