1. 程式人生 > >python裝飾器的用法

python裝飾器的用法

clas ray urn span turn class pan args pytho

def logger(func):

def inner(*args, **kwargs): #1 print "Arguments were: %s, %s" % (args, kwargs) return func(*args, **kwargs) #2 return inner >>> @logger ... def foo1(x, y=1): ... return x * y >>> @logger ... def foo2(): ... return 2 >>> foo1(5, 4)
Arguments were: (5, 4), {} 20 >>> foo1(1) Arguments were: (1,), {} 1 >>> foo2() Arguments were: (), {} 2

python裝飾器的用法