python中装饰器

 

1、

>>> def a():
    print("begin eating!")

    
>>> print("starting!")
starting!
>>> a()
begin eating!
>>> print("ending")
ending

>>> def a():
    print("starting!")
    print("begin eating!")
    print("ending!")

    
>>> a()
starting!
begin eating!
ending!

>>> def a():
    print("begin eating!")

    
>>> def b(x):
    def c():
        print("starting!")
        x()
        print("ending!")
    return c

>>> d = b(a)
>>> d()
starting!
begin eating!
ending!
>>> b(a)()
starting!
begin eating!
ending!

>>> def a(x):
    def b():
        print("starting!")
        x()
        print("ending!")
    return b

>>> @a
def c():
    print("begin eating!")

    
>>> c()
starting!
begin eating!
ending!

>>> def a(x):
    def b(y):
        print("starting!")
        x(y)
        print("ending!")
    return b

>>> @a
def c(y):
    print("%s begin eating!" % y)

    
>>> c("xiao jia yu")
starting!
xiao jia yu begin eating!
ending!

 

posted @ 2021-03-07 10:59  小鲨鱼2018  阅读(51)  评论(0编辑  收藏  举报