是的,您可以使用元类在创建时修改您的类方法,并使用特殊的装饰器装饰每个函数。在你的情况下,它可能是这样的:
#! /usr/bin/env python
#-*- coding:utf-8 -*-
import time
from inspect import isfunction
from functools import wraps
def sleep_decorator(func):
@wraps(func)
def inner(*args, **kwargs):
result = func(*args, **kwargs)
time.sleep(0.1)
return result
return inner
class BaseFoo(type):
def __new__(cls, name, bases, dct):
for name in dct:
if isfunction(dct[name]):
dct[name] = sleep_decorator(dct[name])
return type.__new__(cls, name, bases, dct)
class Foo (object):
__metaclass__ = BaseFoo
def __init__ (self, a, b):
self.a = a
self.b = b
def printer (self, arg):
print arg