Day11-python Foundation

Source: Internet
Author: User

Day11

day11 1

1. Application of Function name (first object) 2

1.1. 1, the direct print function name gets the memory address of the function 2

1.2. 2, function name can be assigned a value Operation 2

1.3. 3, function name can be used as parameter of function 2

1.4. 4, the function name can be used as an element of the container class data type 2

1.5. function names can be used as return values for functions 2

2. Closure Pack 2

2.1. inner function A reference to a non-global variable of outer layer function is called closure 3

2.2. judgment is the name of the closure function. __closure__ 3

2.2.1. None returned is not a closure, the cell is returned .... Is the closure 3

2.3. What's the use of closures? 3

2.3.1. when executing a function, if the interpreter determines that the inner closure of this function exists, Python has a mechanism where the temporary namespace of the closure does not disappear as the function finishes executing. 3

3. Adorner 3

3.1. definition and application 4

3.1.1. adorners are essentially a Python function that allows other functions to add additional functionality without any code changes, and the return value of the adorner is also a function object. 4

3.1.2. Adorner application scenarios: such as inserting logs, performance tests, transaction processing, caching, and so on. 4

3.2. simple to enter and multiply 4

3.2.1. Starter Decorators 4

3.2.2. Syntax sugar 5

3.2.3. Adorner with parameters 5

3.2.4. holding the adorner for all parameters 6

  1. function name Application (first object)

    1. Span style= "FONT-FAMILY:CALIBRI; Font-size:12pt ">1, the direct print function name gets the memory address of the function
    2. 2, the function name can be assigned a value operation
    3. 3, function name can be a parameter of the function
    4. Span style= "FONT-FAMILY:CALIBRI; Font-size:12pt ">4, the function name can be an element of the container class data type
  2. Closed Package

    1. Inner layer function The reference to the non-global variable of the outer layer function is called the closure packet
    2. The judgment is the name of the closure function. __closure__

    1. None returned is not a closure, the cell is returned .... It's a closed packet.
    1. What's the use of closures?

    1. When executing a function, if the interpreter determines that the inner closure of this function exists, Python has a mechanism where the temporary namespace of the closure does not disappear as the function finishes executing.
  3. Decorative Device

      1. Definition and application

      1. The adorner is essentially a Python function that allows other functions to add additional functionality without any code changes, and the return value of the adorner is also a function object.
      2. Application scenarios for adorners: such as inserting logs, performance tests, transaction processing, caching, and so on.
      1. Simple to enter and multiply

    1. Starter Decorators

      Import time

      Def func1 ():

      Print (' in func1 ')

      def timer (func):

      def inner ():

      Start = Time.time ()

      Func ()

      Print (Time.time ()-start)

      return inner

      FUNC1 = Timer (func1)

      Func1 ()

      Decorator---simple version

    2. Grammar Sugar

      Import time

      def timer (func):

      def inner ():

      Start = Time.time ()

      Func ()

      Print (Time.time ()-start)

      return inner

      @timer #==> func1 = Timer (func1)

      Def func1 ():

      Print (' in func1 ')

      Func1 ()

      Decorator---grammar sugar

    3. Adorner with Parameters

      def timer (func):

      def inner (a):

      Start = Time.time ()

      Func (a)

      Print (Time.time ()-start)

      return inner

      @timer

      Def func1 (a):

      Print (a)

      FUNC1 (1)

      Adorner--an adorner with parameters

    4. the adorner holding all parameters

      Import time

      def timer (func):

      def inner (*args,**kwargs):

      Start = Time.time ()

      Re = func (*args,**kwargs)

      Print (Time.time ()-start)

      return re

      return inner

      @timer #==> func1 = Timer (func1)

      Def func1 (A, B):

      Print (' in func1 ')

      @timer #==> Func2 = Timer (FUNC2)

      Def Func2 (a):

      Print (' In Func2 and get a:%s '% (a))

      Return ' fun2 over '

      Func1 (' aaaaaa ', ' bbbbbb ')

      Print (Func2 (' aaaaaa '))

      Adorner---Hold all parameters of the adorner

Day11-python Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.