Pythian Study Notes 6: cyclic objects

Source: Internet
Author: User

In the past, I always wanted to concentrate on learning things. After the National Day holiday, I found that this TM was not practical at all. It was better to spend some time every day.


1. circular object

The circular object does not exist with the birth of Python, But it develops rapidly. Especially in the era of Python 3x, circular objects are becoming the standard form of loops.
A circular object is such an object. It contains a next () method (_ next _ () method, in Python 3x ),
The purpose of this method is to proceed to the next result, and after the end of a series of results, the stopiteration error is cited.
When a loop structure (such as for) calls a loop object, it calls the next () method every time it loops until stopiteration appears,
After receiving the for loop, you will know that the loop has ended and stop calling next ().

2. Simulation
Assume that there is a file test.txt with the following content:
123abcxyz

Now let's read the content one by one:
f = open("test.txt")print f.next()print f.next()print f.next()# print f.next() #Traceback...in:...StopIteration
In the last commented print F. Next (), an error is reported, indicating stopiteration.

Open () actually returns a circular object containing the next () method.
The next () method returns the content of a new row each time, and the stopiteration is cited at the end of the file.
This is equivalent to simulating the following loop:
for line in open("test.txt"):    print line
Or the structure automatically calls the next () method and assigns the return value of this method to line. The loop will end when stopiteration occurs.

Iii. Generator
The main purpose of generator is to create a user-defined cyclic object.
The Writing Method of the generator is similar to the function definition, but the return is changed to yield.
The generator can have multiple yield instances. When the generator encounters a yield, it will pause running the generator and return the value following yield.
When the generator is called again, it will continue running from the paused place until the next yield.
The generator itself forms a loop, and each cycle uses a value returned by yield.

The following is a generator:
def gen():    a = 2    yield a    a = a * 2    yield a    yield 1024
The generator has three yield instances. If it is used as a loop generator, it performs three cycles.
for i in gen():    print i

Let's look at another generator:
def gen():    for i in range(4):        yield i
It can be written as a generator expression:
G = (x for x in range(4))
Generator expressions are a simple way to compile generators.


Iv. Table Derivation
List comprehension is a quick way to generate tables.
For example:
L = []for b in range(10):    L.append(b**2)
The preceding table L is generated, but there is actually a quick writing method, that is, the table deduction method:
L = [b**2 for b in range(10)]print(L)
Output:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Another example:
xl = [1,3,5]yl = [9,12,13]L  = [ x**2 for (x,y) in zip(xl,yl) if y > 10]print(L)
Output:

[9, 25]

Pythian Study Notes 6: cyclic objects

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.