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'
Foo().bar()
bar
Foo().baz()
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:
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'
Foo().bar()
bar
Foo().baz()
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 ?
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
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:
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
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.
On Sun, Jun 6, 2010 at 6:16 PM, David Stanek dstanek@dstanek.com wrote:
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.
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