There are parameters decorator, iterators, generators

Decorator supplement

# Perpetrating a fraud, is about the original function name at the memory address of the function of perpetrating a fraud as a wrapper 
# wrapper so you should do the same job with the original function
# from functools Import Wraps
#
# DEF the outter (FUNC):
# @wraps (FUNC)
# DEF wrapper (* args, ** kwargs):
# "" "this is the Home function" ""
# RES = FUNC (* args, ** kwargs) # RES index = (1,2)
# return RES

# manual original function attribute assigned to the wrapper function
# 1, the function wrapper .__ name__ = primitive .__ name__
# 2, the function wrapper .__ doc__ = primitive .__ doc__ displays a
# wrapper .__ name__ = FUNC .__ name__
# wrapper .__ doc__ = FUNC .__ doc__ displays a

# return wrapper

# @ # = index the outter the outter (index)
# DEF index (the X-, the y-):
# "" "this is the home page feature" ""
# Print (the X-, the y-)

# Print (index.__name__)
# print(index.__doc__) #help(index)

# index(1,2) # wrapper(1,2)

There are parameters decorator

# A: knowledge base 
# @ due to the limitations of syntactic sugar, outter function can only have one argument, and that is only used to receive
# is the memory address of decorative objects
# def outter (func):
Memory # # func = function address
# DEF wrapper (* args, ** kwargs):
# RES = FUNC (* args, ** kwargs)
# return RES
# return wrapper
#
# # # @outter the outter index = (index) index = #> wrapper
# @ the outter # the outter (index)
# DEF index (the X-, the y-):
# Print (the X-, the y-)

after # willed
# what the index's parameters, the parameters for the wrapper should look like
# index return value of what it was like, wrapper return what value should be
# index attributes look like, wrapper attributes should look like == "Import from functools Wraps

# ShanBao play:
# DEF auth (FUNC, db_type):
# DEF wrapper (* args, ** kwargs) :
INPUT name = # ( '>>> your name:') .strip ()
# pwd = INPUT ( '>>> your password:') .strip ()
#
# IF db_type == 'File':
# Print ( ' based authentication file ')
# IF name ==' Egon 'and pwd ==' 123 ':
# RES = FUNC (* args, ** kwargs)
# return RES
# the else:
# Print (' User password or error ')
elif db_type == # 'mysql':
# Print ( 'based authentication of mysql')
# elif db_type == 'ldap':
# Print ( 'based on the authentication ldap')
# the else:
# Print ( 'not supported db_type')
#
# Return wrapper
#
Source # # @auth # account password file
DEF index # (X, Y):
# Print ( 'index - >>% S:% S'% (X, Y))
#
Source # # @auth # account password database
# DEF Home (name):
# Print ( 'Home - >>% S'% name)
#
source # # @auth # account password is the LDAP
# DEF Transfer ():
# Print ( 'Transfer')
#
# = auth index (index, 'File')
Home the auth = # (Home, 'MySQL')
# = Transfer the auth (Transfer, 'LDAP')
#
# # index (1,2)
# # Home ( 'Egon')
# # Transfer ()

# ShanBao two
# def auth (db_type):
# DEF Deco (FUNC):
# warpper DEF (* args, ** kwargs):
# name = INPUT ( '>>> your name:') .strip ()
# pwd = INPUT ( 'your password>>>: ').strip()
#
IF db_type == # 'File':
# Print ( 'based authentication file')
# IF name == 'Egon' and pwd == '123':
# RES = FUNC (* args, ** kwargs)
# return RES
the else #:
# Print ( 'User password or error')
# elif db_type == 'mysql':
# Print ( 'based authentication of mysql')
# elif db_type == 'ldap':
# Print ( 'based authentication ldap' )
# the else:
# Print ( 'not supported db_type')
#
# return wrapper
# return Deco
#
# Deco = auth (db_type = 'File')
# Source @deco # account password file
# def index (x, y) :
Print # ( 'index - >>% S:% S'% (X, Y))
#
# = Deco the auth (db_type = 'MySQL')
# Source @deco # account password database
# def home (name):
Print # ( 'Home - >> S%'% name)
#
# = Deco the auth (db_type = 'LDAP')
# source @deco # account password is LDAP
# DEF Transfer ():
# Print ( 'Transfer')
#
index # (1,2)
# Home ( 'Egon')
# Transfer ()

# syntactic sugar
# DEF the auth (db_type):
# DEF Deco (FUNC):
# warpper DEF (* args, ** kwargs):
# name = INPUT ( '>>> your name:') .strip ()
# pwd = INPUT ( '>>> your password:') .strip ()
#
# IF db_type == 'File':
# Print ( 'based authentication file')
IF name == # 'Egon' and pwd == '123':
# RES = FUNC (* args, ** kwargs) # index (1,2)
# return RES
# the else:
# Print ( 'User password or error' )
# elif db_type == 'mysql':
# Print ( 'based authentication of mysql')
# elif db_type == 'ldap':
# Print ( 'based on the authentication ldap')
# the else:
# Print ( 'not supported db_type ')
# return warpper
# return Deco

# @auth (db_type =' File ') # # @deco Deco index = (index) index = warpper #
# DEF index (X, Y):
# Print (' index -% >> s:% s'% (x ,Y))

@Auth # (db_type = 'MySQL') # # @deco = Home Deco (Home) = # Home warpper
# DEF Home (name):
# Print ( 'Home - >> S%'% name)


# @auth (db_type source = 'ldap') # account password is the LDAP
# DEF Transfer ():
# Print ( 'Transfer')

# index (1, 2)
# Home ( 'Egon')
# Transfer ()

# there is a reference decorator template
# there decorator def reference (X, Y, Z):
# def the outter (FUNC):
# def warpper (* args, ** kwargs):
# RES = FUNC (* args, ** kwargs)
# return RES
# return warpper
return the outter #

# @ decorator with a reference (. 1, Y = 2, Z =. 3)
# DEF decorated objects ():
# Pass

Iterator

'' ' 
1 What is an iterator
iterator refers to the value of the iteration tools, iteration is an iterative process, each iteration
is based on a result of continued, not simply repeat iteration

2, why iterators have
iterator is used to iterative value tool, and a plurality of values related to the type of cycles taken out
are: a list of strings, tuples, dictionaries, set to open the file

l = [ 'egon', 'Liu', 'Alex']
I = 0
the while I <len (L):
Print (L [I])
I + =. 1

above iterative manner value applies only to data type index: a list of strings, yuan group
to address the limitations based on the index value of the iterator
python must be provided without depending on the value of the index mode, which is the iterator


3, how iterator

'' '
# 1, the object may be iterative: built whenever there are methods __iter__ be called iterative object
# S1 = ''
# # S1 .__ ITER __ ()
#
# L = []
# # L .__ ITER __ ()
#
# T = (. 1,)
# # T ITER __ .__ ()
#
# D = { 'A':. 1}
# # D.__iter __ ()
#
Setl = {l, 2,3} #
# # setl .__ ITER __ ()
#
# with Open ( 'a.txt', MODE = 'W') AS F:
# # F .__ ITER __ ()
# Pass

# 2, call __iter__ iterative method can be under the object will convert iterator object
# D = { 'a':. 1, 'B': 2, 'C':}. 3
# d_iterator ITER .__ D = __ ()
# Print ( d_iterator)

# Print (d_iterator .__ the Next __ ())
# Print (d_iterator .__ the Next __ ())
# Print (d_iterator .__ the Next __ ())
# Print (d_iterator .__ the Next __ ()) # thrown StopIteration

# the while True:
# the try:
Print # (d_iterator .__ next __ ())
# the except the StopIteration:
# BREAK
#
# Print ( '==== >>>>>>') # iterator value taken in a clean case, then its value not marry
# d_iterator = d .__ iter __ ()
# the while True:
# try:
Print # (d_iterator .__ Next __ ())
# the except the StopIteration:
# BREAK

# L = [1,2,3,4,5]
# = L .__ l_iterator ITER __ ()
#
# the while True:
# the try:
# Print (l_iterator. __ the __next ())
# the except the StopIteration:
# BREAK

#. 3, iterables the iterator object Detailed
# 3.1 iterables ( "can be converted into an iterator object"): a method __iter__ built objects
# iterables. the __iter __ (): get iterator object

# 3.2 iterator object: a built __next__ method and built __iter__ method object
# iterator object .__ next __ (): get the next value of the iterator
# iterator object. __iter __ (): get the iterator itself, it plainly tune with no tune a look
# DIC = { 'a':. 1, 'B': 2, 'C':. 3}
#
# dic_iterator = DIC .__ ITER __ ()
Print # (dic_iterator IS dic_iterator .__ iter __ () .__ iter __ () .__ iter __ ())
#
# 4, iterables: strings, lists, tuples, dictionaries, set the file object
# iterator object: File Object
# S1 = ''
# S1 .__ ITER __ ()
#
# L = []
# L .__ ITER __ ( )
#
# T = (. 1,)
# ITER __ .__ T ()
#
# D = { 'A':. 1}
# D .__ ITER __ ()
#
# setl = {l, 2,3}
# setl .__ ITER __ ()
#
Open with # ( 'a.txt', MODE = 'W') AS F:
# F .__ ITER __ ()
# F .__ Next __ ()

#. 5, the working principle for loop: for circulation loop may be referred to call the iterator
# D = { 'a':. 1, 'B': 2, 'C':. 3}

#. 1, D .__ ITER __ () to get a iterator object
# 2, iterator object .__ next __ () get a return value , then returns the value assigned to the K
#. 3, step 2 the cycle, until raise StopIteration for loop and end loop to catch exceptions
# for K in D:
# Print (K)

# with Open ( 'a.txt' , mode = 'rt',encoding='utf-8') as f:
# For Line in f: # f .__ iter __ ()
# Print (Line)

# List ( 'the Hello') # principle is the same for cycle

# 6, iterators advantages and disadvantages of summary
# 6.1 shortcoming:
# the I, for the sequence and non-sequence types It provides a unified way of iterative value.
# II, lazy evaluation: iterator object is represented by a data stream, you can only when needed to call the next to calculate a value, it is the iterator itself, only one value at a time in memory, which can store infinite data streams for other types of containers, such as lists, all the elements need to be stored in memory, restricted memory size, the number of values can be stored is limited.

# 6.2 shortcoming:
# the I, unless depletion, or can not get the iterator length
#
# II, can only remove a value, not back to the beginning, more like a 'one-off', the only goal is to produce an iterator repeat next method until the value of depletion, otherwise it will stay in one place, waiting for a call to the next; if you want iterations of the same object again, you can only recall iter method to create a new iterator object, if there is two or more cycles using the same iterator, there will only be bound to one cycle can take values.

Builder

# How to get custom iterators: 
# yield keyword once in the function exists, call the function does not perform the function body of code
# will return an object generator, the generator iterator that is custom
# def func ():
# print ( 'first')
# the yield. 1
# print ( 'second')
# 2 the yield
# print ( 'third')
# the yield. 3
# print ( 'fourth')

# G = FUNC ( )
# Print (G)
# Builder iterators
# G .__ iter __ ()
# G .__ the Next __ ()

# can trigger a function body code, then encounters yield stopped, after the yield value of
# as this call results are returned
# RES1 = G .__ Next __ ()
# Print (RES1)
#
# RES2 = G .__ Next __ ()
# Print (RES2)
#
# RES3 = G .__ Next __ ()
# Print (RES3)
#
# RES4 = G. __ the __next ()

# len ( 'AAA') # 'AAA' .__ len __ ()

# Next (G) # G .__ Next __ ()
# ITER (iterables) # iterables .__ ITER __ ()

# applications text column
# DEF My_Range (Start, STOP, STEP =. 1):
# # Print ( 'Start. .. ')
# Start the while <STOP:
# Start the yield
# = Start + STEP
# Print (' End .... ')


# My_Range G = (1,5,2). 3. 1 #
# Print (Next (G))
Print # (the Next (G))
# Print (the Next (G))

# for the n-in My_Range (1,7,2):
# Print (the n-)

# summary yield:
# With the yield keyword, we have a kind custom implementations of iterators. yield may be used to return values, but unlike the return, the function return is encountered once ended, and the yield can keep a running state function suspend function to return multiple values

Guess you like

Origin www.cnblogs.com/0B0S/p/12558980.html