Python 中的类装饰器 类似于JAVA中的annotation

来源:http://www.fanjun.me/?p=568

扩展阅读:http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html


#encoding=utf8
#http://www.fanjun.me/?p=568
_missing = None


class cached_property(object):


    def __init__(self, func, name=None, doc=None):
        self.__name__ = name or func.__name__
        self.__module__ = func.__module__
        self.__doc__ = doc or func.__doc__
        self.func = func


    def __get__(self, obj, type=None):
        if obj is None:
            return self


        value = obj.__dict__.get(self.__name__, _missing)
        if value is _missing:
            value = self.func(obj)
            obj.__dict__[self.__name__] = value
        return value


class A():


    @cached_property
    def abc(self):
        return 'yes'


b = A()


print b.abc


#要使用下面的方法必须    将cached_property注解了
#print b.abc()

展开阅读全文

没有更多推荐了,返回首页