Get the App
SLTechnology News&Howtos  ›  Development  › 

How to quickly build a Generator for python

Shulou Source: shulou.com Published: 2022-06-03 17:38:01 11月13日 Update

This article will explain in detail how to quickly build a generator for python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Surround the data generation rules in parentheses to create an object of generator type.

A = (x for x in range (10)) print (type (a)) # run result: #

2. Function + yield keyword creation method. You need to write it as a multiline function and return the generator object by calling the function.

The generation rules that you want to represent are complex and cannot be represented by a single line of code.

Def fib (): n = 0a while b = 0je 1m n

Tags: Generation generator function article result run key keyword object more rule good complex practical one line code content parenthesis data article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple vpn NVidia Xiaomi