Home > Backend Development > Python Tutorial > Introduction to the Python keyword yield

Introduction to the Python keyword yield

不言
Release: 2018-10-16 15:59:19
forward
1861 people have browsed it
This article brings you an introduction to the Python keyword yield. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Coroutines are a way to develop asynchronous I/O code in Tornado. The coroutine uses the Python keyword yield to suspend and resume execution of the caller. So before learning about coroutines, we must first familiarize ourselves with the concept and usage of yield. To understand yield, we need to first understand the concept of iterators.

In Python, an iterator defined using the yield keyword is also called a [generator]

1. Iterator

Iterator (Iterator) is a way to access elements in a collection. The iterator object starts accessing the first element of the collection and ends after all elements have been accessed. Iterators cannot go backward, they can only iterate forward.

The most commonly used scenario for iterators in Python is the loop statement for, which encapsulates a collection with an iterator and cooks access to the collection elements to execute the loop.

For example:

for number in range(5):#range返回一个列表
    print(number)
Copy after login

The range() returns a set containing the specified elements, and the for statement encapsulates it into an iterator and then accesses it. Using iter() can talk about lists, The collection is converted into an iterator, for example:

numbers=[1,2,3,4,5]
#t就是迭代器
t=iter(numbers)
#打印t对象,以便查看其类型
print(t)
Copy after login

Return result:

<list_iterator object at 0x10e805748>
Copy after login

Compared with ordinary Python objects, iterators have one more __next__() method, each time Calling this method can return an element, and the caller (such as a for statement) can access the collection elements by continuously calling the __next__() method.

For example:

numbers=[1,2,3,4,5]
#t就是迭代器
t=iter(numbers)
#打印t对象,以便查看其类型
print(t.__next__())
print(t.__next__())
print(t.__next__())
print(t.__next__())
Copy after login

Return result:

1
2
3
4
Copy after login

The caller can keep calling the __next__() method until a StopIteration exception is returned.

2. Use yield

Iterators are widely used in Python programming, so how do developers customize their own iterators?

The answer is to use the yield keyword.

Calling any function defined containing the yield keyword will not execute the function, but will obtain an iterator corresponding to the function.

Example:

import time
def demoIternator():
    print("---1---")
    yield 1
    print("---2---")
    yield 2
    print("---3---")
    yield 3
    print("---4---")

for x in demoIternator():
    print(x)
    time.sleep(1)
Copy after login

Every time the __next__() method of the iterator is called, the iterator function will be executed and the result of yield will be returned as the iteration return element.

The above is the detailed content of Introduction to the Python keyword yield. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template