Superposition decorator:
Superposition decorator
- every new feature should write a new decorator
- otherwise it will lead to redundant code, structure is not clear, poor scalability
In the same object to be decorated, add multiple decorators, and executed.
Decorative @ 1
@ 2 Decorative
@ decoration. 3
DEF decorated objects ():
Pass
Note: decorator in the calling function will be performed when the added decorative objects.
- superimposing decorator:
- decorative sequence: by the lower to the upper trim
- executed sequentially: from top to bottom
Note: Regardless of any judgment appeared in the inner, the last to be returned "after the call is decorative objects" func (* args, ** kwargs)
DEF wrapper1 (FUNC): DEF inner1 (* args, ** kwargs): Print ( ' 1 --- Start ' ) # was decorated object when invoked, if there are other decorators will first perform other decorators in Inner # Inner2 RES = FUNC (* args, ** kwargs) Print ( ' . 1 --- End ' ) return RES return inner1 DEF wrapper2 (FUNC): DEF Inner2 (* args, ** kwargs): Print ( ' 2- --start ' ) RES = FUNC (* args, **kwargs) Print ( ' 2 --- End ' ) return RES return Inner2 DEF wrapper3 (FUNC): DEF inner3 (* args, ** kwargs): Print ( ' . 3 --- Start ' ) RES = FUNC (* args, ** kwargs) Print ( ' . 3 --- End ' ) return RES return inner3 '' ' decorative overlay order of execution order decorators: - decorative sequence: call get return value wrapper decorator inner decoration from bottom to top - execution order: after the call the return value of inner decoration of executing down '' ' @ wrapper1 # index "--- inner1 = wrapper1 (Inner2) @ wrapper2 # Inner2 = wrapper2 (inner3) @ wrapper3 # inner3 = wrapper3 (index ) DEF index (): # was decorated objects inner1 --- # " Print ( ' from index ... ' ) # was decorating index = wrapper3 (index) index = wrapper2 (index) index = wrapper1 (index) '' ' inner1 () Inner2 () inner3 () index () '' ' index () # here to perform # inner1 () -> inner2 ( ) ---> inner3 ()
There are function parameters
DEF USER_AUTH (user_role): # 'SVIP' DEF warpper (FUNC): DEF Inner (* args, ** kwargs): IF user_role == ' SVIP ' : # add root function res = func (* args, ** kwargs) return RES elif user_role == ' Common user ' : Print ( " normal user " ) # add general user function res = func (* args, **kwargs) return RES return Inner return warpper # decorated objects USER_AUTH ( ' SVIP ' ) is equivalent to the following two lines of code warpper = USER_AUTH ( ' normal user ' ) @wrapper # @user_auth ( 'SVIP') # wrapper = user_auth ( ' normal users') # @wrapper # <--- return results (warpper) <---- USER_AUTH () DEF index (): Pass # warpper USER_AUTH = ( 'normal user') is equivalent to two lines # index = wrapper (index) index ()
wraps: (understanding)
is a repair tool to repair the space is decorated objects. Do not show the outer decorative commentary
from functools import wraps
from functools Import Wraps DEF wrapper (FUNC): @wraps (FUNC) # change the name space: inner --- "func do not write here goes decorator comment DEF Inner (* args, ** kwargs): '' ' This at a comment decorator : param FUNC: : return: '' ' RES = FUNC (* args, ** kwargs) return RES return Inner # --- "FUNC @wrapper DEF index (): ' '' here is Note index function : return: '' ' Pass Print (index) #Function object # function object .__ doc__: View inside the function comment Print (index. __Doc__ ) # Inner .__ doc__
Iterator
- iterator
tool iterations.
Iteration:
Iteration refers to the repeated iterations, each iteration is based on results from the first.
Iterator:
iterator refers to the value of the iterative tool that can iterate values.
- If you want to know what python iterator is? You must know what is iterables?
Iterables: All sequence types: str, list, tuple, dict , set, f
all internal str .__ iter __ () method is iterable.
- Get iterator:
by iterables .__ iter __ (), the obtained return value "iterator object."
Iteration is the iteration value of the tool, the role is iterative value.
- How iteration values:
iterator object .__ next __ () # "each execution" will remove a value from the iterator object
- Summary: iterables VS iterator object:
- Get iterables: definition of sequence type (STR, List, SET, tuple, dict, F)
- Features:
built the __iter __ ()
- Get iterator object: object calls via an iterative .__ iter __ () Return value obtained
- Features:
built the __next __ ()
- iterator object advantages:
- advantages:
1. does not depend on the iteration index values.
2. save memory space.
- Cons:
1. Take the trouble to specify a value
2. Each value must start from the first value, not the value of the index over the same.
# The following are iterables ' '' str1 = 'Hello Tank!' Str1 .__ ITER __ () List1 = [. 1, 2,. 3] # List ([. 1, 2,. 3]) List1 .__ ITER __ () SET .__ iter__ () dict .__ ITER __ () tuple .__ ITER __ () Open ( 'a.txt') .__ ITER __ () '' '
# List1 is a iterable List1 = [ ' Tank ' , ' Jason chicken Brother ' , ' Sean ' , ' cake Brother ' ] # Gets iterator object: iter_list1 . Iter_list1 = List1 the __iter__ () the while True: # added: try : catch the exception the try : Print (. iter_list1 __next__ ()) # error # immediately trigger code here StopIteration the except StopIteration: BREAK
测试迭代文件 f = open('user.txt', 'r', encoding='utf-8') iter_f = f.__iter__() while True: try: print(iter_f.__next__()) except StopIteration: break
The for loop nature
Principle for loop
syntax: for i in iterables:
in: iterables ----> Internal automatically calls .__ iter __ () ---> iterator object
for Line in List1:
# iterator object .__ next __ ()
= List1 [. 1, 2,. 3,. 4 ] for Line in List1: # List1 is iterables ----> internal automatically calls .__ iter __ () ---> iterator object # iterator object .__ next __ () Print (Line)
Iterator object
- is an iterative nature object iterator
- the nature of both documents iterator object, also iterable.
- iterables not necessarily iterator object
iter_set1 = setl. the __iter__ () iter_set1. __next__ () # OK: iterator object is a iterable # determines whether iterables is iterator object # Print (iter_set1 .__ ITER __ () IS iter_set1) # True # alone file special: since the time read out from a file is an iterator object # F ---> iterables, or iterator object # F = Open ( 'user.txt', 'R & lt', encoding = 'UTF- . 8 ') # F .__ Next __ () # # # # determine: both iterables file, also iterator object. # # # Iter_f ---> iterator object # iter_f ITER __ .__ = F () # iter_f .__ Next __ () '' ' List1 = [. 1, 2,. 3] # iterator object iter_list = list1 .__ iter __ () = str1 '123' # iterator object iter_str1 .__ = str1 ITER __ () Print (iter_list IS iter_str1) # False '' ' # iterables List1 = [. 1, 2,. 3,. 4 ] # iter_list1 ---> iterator Object iter_list1 = List1. the __iter__ () Print (iter_list1 iS List1) # False # iterables iterator object is not necessarily