Python Foundation 13th Day (iterator, generator)

Source: Internet
Author: User
Tags generator

Today's main content:

1. Can iterate objects

2. iterators

3. Generator

1. Can iterate objects

What is an iterative object: An object that contains the __iter__ method internally, which is an iterative object.

View __iter__ Method: Dir (object)

An iterative object follows an iterative protocol.

The iterative objects that have been exposed to are: Str,list,tuple,dict,set,range

Iterator: F1 file handle

How to judge

Law One:

Example: s = ' Laonanhai '

Print (' __iter__ ' in Dir (s))

Law II:

Example: L = [1, 2, 3, 4]
From collections Import Iter

Print (Isinstance (l, iterable))

return Result: # True

2. iterators

What is an iterator

An iterative object can be transformed into an iterator: an object can be iterated. __ITER__ ()---> iterators

Not only contains the __iter__ method, but also contains the __next__ method

Example: L1 = [+ +]

L1_obj = l1.__iter__ ()

Print (' __iter__ ' in Dir (l1_obj)) #True

Print (' __next__ ' in Dir (l1_obj)) #True

To determine the iterator:

Law One:

Print (' __iter__ ' in Dir (l1_obj)) #判断是否是可迭代对象
Print (' __next__ ' in Dir (l1_obj)) #判断是否为迭代器

Law II:
From collections Import Iterator
Print (Isinstance (l1_obj, Iterator))

Benefits of Iterators:

1. Save memory space, only execute the __NEXT__ function, the next step, and load into memory.

2. Meeting Inert machines

3. Can not be repeated value, irreversible.

Example: Simulating a For loop internal mechanism with a while loop

Ideas:

‘‘‘

1. Convert an iterative object into an iterator

2. Internal use of __next__ method, value

3. Using exception handling to deal with the error

‘‘‘

Li = [1,2,3,4,5,6,7]

Li_obj = li.__iter__ ()

While True:

Try

i = li_obj.__next__ ()

Printe (i)

Except Exception:

Break

# Exception means all non-code errors

3. Generator

(1)

What is a generator: The generator is essentially an iterator and is a special iterator. So the iterator has a bit of it.

(2)

How generators are produced:

1. Builder function Constructs

2. Generator derivation constructs

3. Conversion of data types

1. Builder function Constructs

Method:

The function can be transformed into a generator by turning return to yield under the normal function.

Cases:

Def FUNCL ():

Print (666)

Print (555)

Return 777

Print (FUNCL ())

Def FUNCL1 ():

Print (666)

Print (555)

Yield 444

Yleld 777

g = Funcl ()

Print (g)

Conclusion: # <generator object func1 at 0x0000000001197888>

Analytical:

First: As long as there is yield in the function, he is not a function, but a generator.

Second: G is called the generator object

Perform:

Print (g.__next__ ())

Print (g.__next__ ())

Print (g.__next__ ()) #超出yield数量, error

The role of sent

Next, like the Send function, is performed once
Send can assign a value to the previous yield

Cases:

Def generator ():
Print (123)
Content = Yield 1
Print (content)
Print (456)
Yield 2
g = Generator ()
G.__next__ ()
G.send (' Hello ')

Python Foundation 13th Day (iterator, generator)

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.