python 装饰器

了解装饰器之前请深入了解闭包。

class Coordinate(object):
'''
坐标(x,y)
'''
def __init__(self, x, y):
self.x = x
self.y = y

def __repr__(self):
return "coord: " + str(self.__dict__)
#func = wrapper(func)
def wrapper(func):
def check(a,b):
res = func(a, b)
res = Coordinate(res.x if res.x >0 else 0,res.y if res.y > 0 else 0)
return res
return check

@wrapper
def addd(a, b):
return Coordinate(a.x +b.x, a.y +b.y)

@wrapper
def subb(a, b):
return Coordinate(a.x -b.x, a.y-b.y)
原文地址:https://www.cnblogs.com/homie/p/9512648.html