码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器

时间:2017-05-25 01:22:52      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:return   int   class   blog   after   highlight   module   python   name   

有参数的装饰器

def deco(arg):           #装饰器的参数
    def _deco(func):     #函数名
        def __deco(x):   #函数的参数
            print("before %s called [%s]." % (func.__name__, arg))
            s = func(x)
            print("after %s called [%s]." % (func.__name__, arg))
            return s
        return __deco
    return _deco

@deco("module")
def myfunc(x):
    print("myfunc() called.")
    return x ** 2

s = myfunc(14)
print(s)

# before myfunc called [module].
# myfunc() called.
# after myfunc called [module].
# 196

  

装饰器

标签:return   int   class   blog   after   highlight   module   python   name   

原文地址:http://www.cnblogs.com/yangxiaoling/p/6901581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!