Python Learning Notes-(eight) decorators, generators & iterators

Source: Internet
Author: User

1. Decorative Device 1.1 Definition:

essentially a function that decorates other functions-that is, adding additional functionality to other functions

1.2 Adorner principle:

1) cannot modify the source code of the decorated function;

2) cannot modify the calling mode of the decorated function;

1.3 Implement the Adorner knowledge Reserve:

The 1.3.1 function is the "variable"

Defining a function is equivalent to assigning a function body to a functional name.

1.3.2 Higher order functions

See the previous study notes

1.3.3 Nested Functions

Definition: Use DEF to declare a new function in a function body

# def foo ():         #定义函数foo () ...    m=3            #定义变量m =3;    ... def bar ():     #在foo内定义函数bar ()        n=4        #定义局部变量n =4 ...        print (m+n)  #m相当于函数bar () global variables ...    bar ( )         #foo () function call function Bar () # foo ()             #调用foo () function 7
1.3.4 Decorator

Higher order function + nested function = = Adorner

    • Fixed function parameters
# import time# def timer (func):    ... def deco (name):        ... start_time = Time.time () ...        func (name)        ...  Stop_time = Time.time () ...        print ("The Func Run time is%s"% (stop_time-start_time)) ...    return deco# @timer # def Test (name):    ... time.sleep (3) ...    print ("In the test!--%s"% name) # test (' CC ')

    • Function parameters are not fixed

2. Iterators & Generators 2.1 List-generated 2.2 iterators 2.3 Generators 3.json&pickle serialization of data 4. Program Catalog Specification

Python Learning Notes-(eight) decorators, generators & iterators

Related Article

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.