Python notes basic article-day4

Source: Internet
Author: User

One. Generator

Generator generator
If the function contains yield syntax, then this function will become the generator, the yield of the main effect is that the function can be interrupted, and save the interrupt state, after the interruption, the code can continue to execute, after a period of time can also recall the function, from the last yield of the next sentence to start execution.

Example:
Import time
def consummer (name):
Print ("%s starts eating buns"% name)
While True:
Baozi =yield #yield既可以返回暂停一下, you can also accept the value sent by send, including yield is the generator, method next pause to 19 rows
Print ("Bun%s has come, eaten by%s"% (Baozi,name))

def producer (name2):
C1 = Consummer ("Jack") #调用函数1
C2 = Consummer ("Tom")
c1.__next__ () #consummer函数返回的是迭代器 back to line 9
C2.__next__ ()
Print ("Start making buns")
For I in range (10):
Time.sleep (1) #每一秒钟做2个包子 If this line is commented, it will be output all at once.
Print ("%s made 2 Buns"% name2)
C1.send ("HI")
C2.send (i) #回到12行

Producer ("Alex") #调用函数2


Two. iterators

Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished.
Iterators can only move forward without backing up, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared beforehand.
An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed.

Example:
Import time
DEF consumer (name):
Print ('%s ready to eat Buns '%name)
While True:
Baozi=yield
Print (' Bun [%s] came, was [%s] ate '% (baozi,name))

def producer (name):
C=consumer (' A1 ')
C2=consumer (' A2 ')
C.__next__ ()
C2.__next__ ()
Print ("Get ready to eat steamed buns")
For I in range (10):
Time.sleep (1)
Print (' Made of 2 buns ')
C.send (i)
C2.send (i)
Producer (' Xym ')

Three. Decorative Device

Role: Extend new functionality to existing functions

Traditional notation:

def login (func):
def inner (ATG):
Print ("passwed user verification ...")
Func (ATG)
return inner


def home (name):
Print ("Welcome [%s] to home page"% name)
def TV (name):
Print ("Welcome [%s] to TV page"%name)

def movie (name):
Print ("Welcome [%s] to movie page"% name)
TV = login (TV)
TV (123)
Print (50* "-+")
Home = Login (Home)
Home (456)
Print (50* "-+")
Movie = login (Movie)
Movie (999)

----------------------------------------------
Output:
passwed User Verification ...
Welcome [123] to TV page
--------------------------------------------------
passwed User Verification ...
Welcome [456] to home page
--------------------------------------------------
passwed User Verification ...
Welcome [999] to Movie page

Decorator notation:


def login (func):
def inner (ATG):
Print ("passwed user verification ...")
Func (ATG)
return inner

@login
def TV (name):
Print ("Welcome [%s] to TV page"%name)
TV (123)

---------------------------------
Output:
passwed User Verification ...
Welcome [123] to TV page


Implement complex with multiple parameter adorners


def login (func):
def inner (*atg,**arg):
Print ("passwed user verification ...")
Func (*atg,**arg)
return inner
@login
def TV (name,passwd= "):
Print ("Welcome [%s] to TV page"%name)
TV (123,passwd = 321)
--------------------------------------
Output
passwed User Verification ...
Welcome [123] to TV page

Adorner return value


#定义一个装饰器
def login (func):
def inner (*atg,**arg):
Print ("passwed user verification ...")
return func (*atg,**arg)
return inner

@login
def TV (name,passwd= "):
Print ("Welcome [%s] to TV page"%name)
Return 4
t = TV (123,passwd = 321)
Print (t)
-------------------------------------
Output:
passwed User Verification ...
Welcome [123] to TV page
4

Define a default adorner and then pass in the function to the adorner, because writing an adorner costs more than writing a function


def before (Request,kargs):
print ' Before '

def after (Request,kargs):
print ' after '


def Filter (Before_func,after_func):
def outer (Main_func):
def wrapper (Request,kargs):

Before_result = Before_func (Request,kargs)
if (before_result! = None):
return before_result;

Main_result = Main_func (Request,kargs)
if (main_result! = None):
return main_result;

After_result = After_func (Request,kargs)
if (after_result! = None):
return after_result;

Return wrapper
return outer

@Filter (before, after)
def Index (Request,kargs):
print ' index '

Python notes basic article-day4

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.