zoukankan      html  css  js  c++  java
  • 装饰器

     

    定义一个函数:

    def outer():
      def inner():
        print("this is first func")
        print("this is end func")
      return inner

    t=outer()
    print(t)
    t()

     """

    <function outer.<locals>.inner at 0x00000000006D01E0>
    this is first func
    this is end func

    """

     

     

    在不改变原来函数的情况下 怎么样在

    print("this is first func")
    print("this is end func")

     之间加一个功能呢!!

     

     

    def outer(fun):
      def inner():
        print("this is first func")
        fun()
        print("this is end func")
      return inner

    def fun():
      print("this is a new func")

    fun=outer(fun)

    print(fun)
    print(fun())

    """

    <function outer.<locals>.inner at 0x0000000000B90268>
    this is first func
    this is a new func
    this is end func
    None

    """

     

    使用装饰器:

    def outer(fun):
      def inner():
        print("this is first func")
        fun()
    print("this is end func")
    return inner

    @outer    # ===>  fun=outer(fun)
    def fun():
      print("this is a new func")

    fun()

    """

    this is first func
    this is a new func
    this is end func

    """

     

  • 相关阅读:
    洛谷 P1092 虫食算
    2018.3.25校内互测
    洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
    洛谷 P1879 [USACO06NOV]玉米田Corn Fields
    洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
    ZJOI Day 2 游记
    editorial-render A
    BZOJ2904
    BZOJ 1600
    构造脚本语言
  • 原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/5911007.html
Copyright © 2011-2022 走看看