• Python基础 装饰器


    装饰器(decorator)

    1. 函数基础
      # function.py  函数
      # 1)函数是对象
      def hello():
          return 'Hello, world!'
      
      func = hello
      print(func)  # <object>
      print(func())
      
      
      # 2)函数可以嵌套,定义在另一个函数内部
      def show():
          print('Run in show()')
          def message(word='Hello'):
              return word
          print(message())
      
      show()
      
      
      # 3)函数作为参数返回
      def getWordType(kind=''):
          def default(word=''):
              return word
          def upper(word=''):
              return word.upper()
          def lower(word=''):
              return word.lower()
      
          if kind.lower() == 'lower':
              return lower
          elif kind.lower() == 'upper':
              return upper
          else:
              return default
      
      wordType = getWordType('upper')
      print(wordType)
      print(wordType('Resistance is futile!'))
      
      
      # 4)函数作为参数传入
      def getName(name='leo'):
          return name
      
      def foo(func):
          print('I will call the getName function later')
          print(func())
      
      foo(getName)
    2. 装饰器以及函数比较
      # decorator.py  装饰器
      # 不使用装饰器情况
      def my_new_decorator(a_function_to_decorator):
          def the_wrapper_around_the_original_function():
              print('Befor the function runs')
              a_function_to_decorator()
              print('After the function runs')
          return the_wrapper_around_the_original_function  # 将函数作为参数返回
      
      
      def a_stand_alone_function():
          print("I'm a stand alone function, don't you dare modify me.")
      
      
      a_stand_alone_function()
      a_stand_alone_function_decorated = my_new_decorator(a_stand_alone_function)  # 将函数作为参数传入
      print(a_stand_alone_function_decorated)
      a_stand_alone_function_decorated()
      
      # 使用装饰器 语法糖"@"
      @my_new_decorator
      def another_stand_alone_function():
          print('leave me alone')
      
      
      another_stand_alone_function()
      # same as: another_stand_alone_function = my_new_decorator(another_stand_alone_function)
    3. 装饰器应用
      # decorator_web.py  web中的装饰器应用
      def makebold(func):
          def wrapper():
              return '<b>' + func() + '</b>'
          return wrapper
      
      def makeitalic(func):
          def wrapper():
              return '<i>' + func() + '</i>'
          return wrapper
      
      @makebold
      @makeitalic
      def word():
          return 'Hello, decorator!'
      
      
      print(word())
      # decorator_time.py  使用装饰器检查函数的运行时间
      import time
      import os
      
      
      def timer(func):
          def wrapper(cwd):
              time_begin = time.ctime()
              print('Begin @' + time_begin)
              func(cwd)
              time_end = time.ctime()
              print('End @' + time_end)
      
              print('Begin @' + time_begin)
              print('End @' + time_end)
          return wrapper
      
      
      @timer
      def walk(cwd=r'D:PyCharmpythonpractice'):
          for root, dirs, files in os.walk(cwd):
              # print(root)  # 打印当前目录下的所有子目录的绝对路径
              # print(dirs)  # 打印每个目录下的子目录列表
              # print(files)  # 打印所有目录下文件列表
              print(root)
              for file in files:
                  print('	' + file)
      
      
      walk(cwd=r'D:PyCharm')
    4. 包含所有参数以及带返回值的装饰器
      # decorator_args.py  包含所有参数的装饰器
      import time
      def timer(func):
          def inner(*args, **kwargs):
              start = time.time()
              re = func(*args, **kwargs)
              end = time.time()
              return re
          return inner
      
      
      @timer  #==> func1 = timer(func1)
      def func1(a, b):
          print('in func1')
          return a, b
      
      
      @timer #==> func2 = timer(func2)
      def func2(a):
          print('In func2 and get', a)
          return 'func2 over'
      
      
      func1('aaaaaa', 'bbbbbb')
      print(func2('aaaaaa'))
    5. 使被装饰的函数能够使用正常函数方法
      # decorator_method.py
      from functools import wraps
      
      def deco(func):
          @wraps(func)
          def wrapper(*args, **kwargs):
              return func(*args, **kwargs)
          return wrapper
      
      @deco
      def index():
          '''docs here in decorator'''
          print('from index')
      
      
      index()
      print(index.__name__)
      print(index.__doc__)
    Resistance is Futile!
  • 相关阅读:
    Oracle10g之SGA与PGA分配建议
    Oracle 用户权限管理方法
    快速部署RDA Remote Diagnostic Agent
    转 :Oracle 数据库信息收集工具RDA使用指南 Oracle 数据库信息收集工具RDA使用指南
    AIX6.1下配置Nmon性能工具
    优化 AIX 6.1 的性能
    使用Oracle外部表来查询分析警告日志
    qq
    Java字符串2
    java字符串
  • 原文地址:https://www.cnblogs.com/noonjuan/p/10956619.html
Copyright © 2020-2023  润新知