Python Learning Day 13th: Parametric decorators and iterators

Source: Internet
Author: User
Tags ldap repetition wrapper

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

Related Article

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.