Adorner with parameters: it is to wrap a layer of functions outside the original adorner
def auth (driver= ' file '): def auth2 (func): def wrapper (*args,**kwargs): name=input ("User:") pwd= Input ("pwd:") if Driver = = ' file ': if name = = ' Egon ' and pwd = = ' 123 ': print (' login successful ') Res=func (*args,**kwargs) return res elif Driver = = ' LDAP ': print (' LDAP ') return wrapper return auth2
@auth (driver= ' file ') def foo (name): print (name) foo (' Egon ')
iterators: tools for iterating over values
1. is an iterator?
Iteration is a repeating process, and each repetition is based on the previous result.
Note: Simple repetition is not an iterative
2. Why do I have iterators?
An index-based iterative approach is only appropriate for lists, tuples, string types, and for dictionaries, collections, and files that do not have an index, and are no longer applicable.
So you have to find a way to not depend on the index value, which is the iterator
Iterators are suitable for iterative objects
3. What is an iterative object?
Every object in Python that has a built-in __iter__ method is an iterative object
An iterative object contains: strings, lists, tuples, dictionaries, collections, and files
4. What is an iterator object?
An iterator object refers to an object that has a built-in __iter__ method and a built-in __next__ method.
The __iter__ method that executes an iterative object gets the built-in iterator object
The file itself is an iterator object
The __iter__ method of the iterator object gets the iterator itself, in order to facilitate the For loop
The __next__ method of the Iterator object gets the next value
Once the iterator is taken clean, then the fetch will be thrown stopiteration
5.for Loop:
The nature of the For loop is the iterator loop
Info = [1,2,3,4]
For k in info:
Print (k)
In followed by must be an iterative object, the for loop in will automatically convert an iterative object into an iterator object
Summarize:
Pros: 1. Provides a common, non-dependent index-based approach to iterative values
2. More space-saving iterator objects
Cons: 1. The value of the iterator is not as flexible as the index and key, and can only be taken backwards
2. Cannot predict the number of iterator values
Python Learning Day 13th: Parametric decorators and iterators