def small(func):
    def getfunc():
        print "<small>"
        func()
        print "</small>"
    return getfunc
def strong(func):
    def getfunc():
        print "<strong>"
        func()
        print "</strong>"
    return getfunc
def text(text="HELLO WORLD"):
    print text
    
# newtext=small(strong(text))
# newtext()

@small
@strong
def text1(text="HELLO WORLD"):
    print text
text1()

 被装饰的函数带参数的范例

#encoding=utf-8
def first(func):
    def _first(hello):
        print "first"
        func(hello)
        print "last"
    return _first
   
@first
def myfunc(hello):
    print hello
myfunc("hello")

 装饰器带参数的范例,这个需要在装饰器定义时多嵌套一层函数:

#encoding=utf-8
def deco(deco_params):
    def _deco(func):
        def _deco(func_params):
            print "first"
            print deco_params
            func(func_params)
            print "last"
        return _deco
    return _deco
       
@deco("params")
def myfunc(func_params):
    print func_params
myfunc("hello")

 

相关文章:

  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-07-03
  • 2022-01-10
  • 2022-02-20
  • 2022-12-23
猜你喜欢
  • 2021-09-19
  • 2021-09-13
  • 2021-12-26
  • 2021-12-20
  • 2021-05-22
  • 2021-06-08
  • 2021-10-08
相关资源
相似解决方案