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

    """

     

  • 相关阅读:
    Kostya the Sculptor
    Parade
    zoj 1097 普吕弗序列
    API分析——Jquery UI Dialog
    伸缩性和可用性反模式(转)
    可伸缩性最佳实战(转)
    二叉索引树BIT
    RMQ
    线段树(区间树)
    双栈计算算术表达式
  • 原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/5911007.html
Copyright © 2011-2022 走看看