import functools
import types
def function_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
print("wrapper", f, args, kwargs)
return f(*args, **kwargs)
return wrapper
def class_decorator(cls):
for name in dir(cls):
attr = getattr(cls, name)
if isinstance(attr, types.FunctionType):
setattr(cls, name, function_decorator(attr))
return cls
@class_decorator
class Yoba:
x = 1
y = 2
def __init__(self):
print("Yoba.__init__")
def foo(self):
print("Yoba.foo")
def bar(self):
print("Yoba.bar")
yoba = Yoba()
yoba.foo()
yoba.bar()
aW1wb3J0IGZ1bmN0b29scwppbXBvcnQgdHlwZXMKCgpkZWYgZnVuY3Rpb25fZGVjb3JhdG9yKGYpOgoKICAgIEBmdW5jdG9vbHMud3JhcHMoZikKICAgIGRlZiB3cmFwcGVyKCphcmdzLCAqKmt3YXJncyk6CgogICAgICAgIHByaW50KCJ3cmFwcGVyIiwgZiwgYXJncywga3dhcmdzKQogICAgICAgIHJldHVybiBmKCphcmdzLCAqKmt3YXJncykKCiAgICByZXR1cm4gd3JhcHBlcgoKCmRlZiBjbGFzc19kZWNvcmF0b3IoY2xzKToKCiAgICBmb3IgbmFtZSBpbiBkaXIoY2xzKToKCiAgICAgICAgYXR0ciA9IGdldGF0dHIoY2xzLCBuYW1lKQogICAgICAgIGlmIGlzaW5zdGFuY2UoYXR0ciwgdHlwZXMuRnVuY3Rpb25UeXBlKToKCiAgICAgICAgICAgIHNldGF0dHIoY2xzLCBuYW1lLCBmdW5jdGlvbl9kZWNvcmF0b3IoYXR0cikpCgogICAgcmV0dXJuIGNscwoKCkBjbGFzc19kZWNvcmF0b3IKY2xhc3MgWW9iYToKCiAgICB4ID0gMQogICAgeSA9IDIKCiAgICBkZWYgX19pbml0X18oc2VsZik6CgogICAgICAgIHByaW50KCJZb2JhLl9faW5pdF9fIikKCiAgICBkZWYgZm9vKHNlbGYpOgoKICAgICAgICBwcmludCgiWW9iYS5mb28iKQoKICAgIGRlZiBiYXIoc2VsZik6CgogICAgICAgIHByaW50KCJZb2JhLmJhciIpCgoKeW9iYSA9IFlvYmEoKQp5b2JhLmZvbygpCnlvYmEuYmFyKCkK