Superposition decorator iterator

Superposition decorator:

In the same object to be decorated, add multiple decorators, and performs

@ Decoration 1

@ Decoration 2

@ Decoration 3

def decorated objects ():

​ pass

Note: decorator calling function is decorative objects will be added

Superposition decorator:

- the order of the decorator: from the bottom to top

- The order of execution: from the bottom to top

Note: Regardless of any judgment appeared in the inner, the last to be returned "after the call is decorative objects" func (* args, ** kwargs)

There are parameters decorator:

Essentially no reference in the decorative outer sheath of a function

No reference decorator can refer to the outer function's name

application:

    用户权限的认证

Iterator

Iterator:

The value of the iteration tools

Iteration:

Refers to iteratively repeated iterations, each iteration is based on a result of generating

Iterator:

Iterator refers to the value of the iterative tool that can be iterated values

  • If you want to know what python iterator is, you first need to know what is iterables?

Iterables:

All sequence types: str (string), list (list), tuple (ancestral), dict (dictionary), set (collection), f (file)

Any internal __ iter__ () method is iterable

Iterables:

By iterables __ iter __ (), the obtained return value "iterator object"

Iteration is the iteration value of the tool, it is to value iteration

Gets an iterator:

list1 = [1,2,3]

#调用__iter _ ()会得到一个返回值,该返回值就是一个迭代器对象

​ iter_list = list1._iter ()

--- How iteration values:

Iterator object __ next__ () Each time, will remove a value from the iterator object

Note: Take the last value, if the value is not, the error

while True
    try:
        print(iter_list._next_())
    except StopIteration:
        break

for loop principle:

in: iterables will automatically call _ iter _ () becomes the iterator object

for i in iterables:

# Will automatically help you call _ next_

print (i): for the inner loop will have a mechanism to catch the exception, in the event of abnormal values ​​will stop

Iterator advantages and disadvantages:

Advantages: to provide a value of the index is not dependent manner

Save memory space

Disadvantages: the value specified in trouble, each value should start from the first look

Can not be calculated length len

Guess you like

Origin www.cnblogs.com/127-2933/p/11867940.html