zoukankan      html  css  js  c++  java
  • 第23课 装饰器

    1、函数里面定义函数:

      1---调用函数里面函数的方法----foo()()或者赋值给一个变量

        inner = foo()

        inner()

        等价于 foo()()

    2、函数里面定义类

    3、装饰器

      1---定义类的静态方法时就使用了装饰器

      @staticmethod

      def jump():

        print('3 meters high')

      2---装饰器的特点是用一个@开头的字符串

      3---在我们阅读别人代码时,会经常碰到装饰器

      4---装饰器通常用来装饰函数

      5---装饰器主要用来给函数增加一点功能

      6---一般装饰器本身也是一个函数(callable)

      7---我们可以想象成---它包含了被装饰的函数

    4、有参数的函数,要装饰的函数参数都不同

      1---以后用装饰器的时候,定义参数都要用*args**kargs这样的形式。这样就可以传任何形式的参数。

    5、装饰器本身带参数----需要的结尾带不同参数的情况

    1-1 函数里面定义函数

    def foo():
        def bar():
            print('in bar()')
        
        print('in foo()')  # 先执行这条语句,因为它在foo()中
       
        bar()   #调用bar()函数,这时执行print('in bar()')语句
    
    foo()
    
    
    执行结果:
    D:Pythonpython.exe "D:/Programs/HelloWorld2/songqin/python/lesson55 装饰器/lesson55 知识点.py"
    in foo
    in bar()
    
    Process finished with exit code 0

    2-1  函数里面定义类,My的有效范围在函数foo()内部

    def foo():
        class My():
            pass
        print('in foo()')
    
        My()
    
    foo()
    
    
    执行结果:
    D:Pythonpython.exe "D:/Programs/HelloWorld2/songqin/python/lesson55 装饰器/lesson55 知识点.py"
    in foo()
    
    Process finished with exit code 0

    3-1 装饰器

     1 def endsign(func):
     2     def wrapper():
     3         return func() + '!!'   #这里的fun后面为什么加(),加()就是调用。
     4     
     5     return wrapper
     6 
     7 
     8 
     9 @endsign    #把hello()当做参数传给func
    10 
    11 def hello():
    12     return 'hello'
    13 
    14 print(hello())   #这里的hello()相当于wrapper()
    15 
    16 
    17 执行结果:
    18 
    19 D:Pythonpython.exe "D:/Programs/HelloWorld2/songqin/python/lesson55 装饰器/lesson55 知识点.py"
    20 hello!!
    21 
    22 Process finished with exit code 0

    4-1  有参数的函数,要装饰的函数参数不同

    执行print(hello('cat', arg2 = 'dog'))时,会把hello('cat', arg2 = 'dog')传给wrapper,所以先执行wrapper函数里面的内容

    return  func(*args, **kargs) 这一句,才会执行hello()里面的内容

    使用装饰器,定义参数的时候用*args和**kargs,这样传参数更灵活

     1 def endsign(func):
     2     def wrapper(*args, **kargs):
     3         print('args',args)
     4         print('kargs', kargs)
     5         return  func(*args, **kargs) + '!!'
     6 
     7     return wrapper
     8 
     9 @endsign
    10 def hello(arg1, arg2 = ''):
    11     print('arg1',arg1)
    12     print('arg2',arg2)
    13     return 'hello %s %s' %(arg1, arg2)
    14 
    15 @endsign
    16 def goodbye(targets):
    17     return 'goodbye %s' % ''.join(targets)
    18 
    19 
    20 print(hello('cat', arg2 = 'dog'))
    21 print(goodbye(['jack', 'tom', 'lisa', 'mike']))
    22 
    23 
    24 执行结果:
    25 
    26 D:Pythonpython.exe "D:/Programs/HelloWorld2/songqin/python/lesson55 装饰器/lesson55 知识点.py"
    27 args ('cat',)
    28 kargs {'arg2': 'dog'}
    29 arg1 cat
    30 arg2 dog
    31 hello cat dog!!
    32 args (['jack', 'tom', 'lisa', 'mike'],)
    33 kargs {}
    34 goodbye jacktomlisamike!!
    35 
    36 Process finished with exit code 0

    5-1  装饰器本身带参数-----需要的结尾带不同参数的情况

     1 def endsign(tail):
     2     def innerOne(func):
     3         def wrapper():
     4             return func() + ' ' + tail
     5         return wrapper
     6     return innerOne
     7 
     8 @endsign('??')
     9 
    10 def hello():
    11     return 'hello'
    12 
    13 print(hello())
    14 
    15 
    16 
    17 执行结果:
    18 D:Pythonpython.exe "D:/Programs/HelloWorld2/songqin/python/lesson55 装饰器/lesson55 知识点.py"
    19 hello ??
    20 
    21 Process finished with exit code 0
  • 相关阅读:
    228. Summary Ranges
    227. Basic Calculator II
    224. Basic Calculator
    222. Count Complete Tree Nodes
    223. Rectangle Area
    221. Maximal Square
    220. Contains Duplicate III
    219. Contains Duplicate II
    217. Contains Duplicate
    Java编程思想 4th 第4章 控制执行流程
  • 原文地址:https://www.cnblogs.com/nick1998/p/13258389.html
Copyright © 2011-2022 走看看