python装饰器

 

'''
AOP之类装饰器
'''
#标准
def standard(class_):
    class_.standard=class_
    print(class_.name)
    return class_
#非标准
def nonstandard(based_on):
    def concrete_decorator(class_):
        class_.standard=based_on
        print(class_.name)
        return class_
    return concrete_decorator

class Unit:
    factor=1.0
    @classmethod
    def value(class_,value):
        if value is None:
            return None
        return value/class_.factor

    @classmethod
    def convert(class_,value):
        if value is None:return None
        return value*class_.factor

@standard
class INCH(Unit):
    """inch"""
    name="in"

@nonstandard(INCH)
class FOOT(Unit):
    """foot"""
    name='ft'
    factor=1/12

 

posted @ 2016-04-28 17:06  similarface  阅读(155)  评论(0编辑  收藏  举报