The cleanest way to do this, or to do other modifications to a class definition, is to define a metaclass Alternatively, just apply your decorator at the end of the class definition: class Something: def foo(self): pass for name, fn in inspect. Getmembers(Something): if isinstance(fn, types. UnboundMethodType): setattr(Something, name, decorator(fn)) For Python 3 replace types.
UnboundMethodType with types. FunctionType In practice of course you'll want to apply your decorator more selectively, and as soon as you want to decorate all but one method you'll discover that it is easier and more flexible just to use the decorator syntax in the traditional way.
The cleanest way to do this, or to do other modifications to a class definition, is to define a metaclass. Alternatively, just apply your decorator at the end of the class definition: class Something: def foo(self): pass for name, fn in inspect. Getmembers(Something): if isinstance(fn, types.
UnboundMethodType): setattr(Something, name, decorator(fn)) For Python 3 replace types. UnboundMethodType with types.FunctionType. In practice of course you'll want to apply your decorator more selectively, and as soon as you want to decorate all but one method you'll discover that it is easier and more flexible just to use the decorator syntax in the traditional way.
I totally agree, I'm also much more comfortable with the explicit decorating of every function rather than a magic implicit decorator. Was just curious is all. – dochead Aug 12 '10 at 13:57.
You could override the __getattr__ method. It's not actually attaching a decorator, but it lets you return a decorated method. You'd probably want to do something like this: class Eggs(object): def __getattr__(self, attr): return decorate(getattr(self, `_` + attr)) There's some ugly recursion hiding in there that you'll want to protect against, but that's a start.
Everytime you think of changing class definition, you can either use the class decorator or metaclass. E.g. Using metaclass import types class DecoMeta(type): def __new__(cls, name, bases, attrs): for attr_name, attr_value in attrs.iteritems(): if isinstance(attr_value, types.
FunctionType): attrsattr_name = cls. Deco(attr_value) return super(DecoMeta, cls). __new__(cls, name, bases, attrs) @classmethod def deco(cls, func): def wrapper(*args, **kwargs): print "before",func.
Func_name func(*args, **kwargs) print "after",func. Func_name return wrapper class MyKlass(object): __metaclass__ = DecoMeta def func1(self): pass MyKlass(). Func1() Output: before func1 after func1 Note: it will not decorate staticmethod and classmethod.
The cleanest way to do this, or to do other modifications to a class definition, is to define a metaclass. For Python 3 replace types. UnboundMethodType with types.FunctionType.
In practice of course you'll want to apply your decorator more selectively, and as soon as you want to decorate all but one method you'll discover that it is easier and more flexible just to use the decorator syntax in the traditional way.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.