当前位置:网站首页>Difference between return and yield
Difference between return and yield
2022-06-26 04:51:00 【I am a little monster】
return: Used in functions to return a value , After returning this value, the program will not continue to execute , A function returns a value directly and ends
>>> def lll():
... return 10
... print(' I am a little monster ')
...
>>> g=lll()
>>> print(g)
10yield: Generate a number , But it doesn't return directly , Instead, generate a generator
As shown in the following code , We can think of the code running to yield Get a generator object at g, Stop here , We have to call next Method to keep the code running , While using next After method , Continued to run... In the function yield Statement returns a value or variable , Next time I meet yield And stop again , This is a bit like an iteration , We must rely on next Go to the next step
In the following code , When using yield 6 to r When you assign , We can see the variables in the output r It shows a null value , in other words yield 6 Returned when , No assignment to variable r, So we use next Method to run the output statement r It's a null value , So we need to use send Method , Pass a value to the function to be run print('s:',s) sentence , So the output gets s Right behind send Value delivered 10
>>> def ret():
... r=yield 6
... print('r:',r)
... print(' This output statement is to let you clearly understand next Where to stop after running ')
... s=yield 8
... print('s:',s)
...
>>> g=ret()
>>> print(g)
<generator object ret at 0x000001A4C1BCB890>
>>> print(next(g))
6
>>> print(next(g))
r: None
This output statement is to let you clearly understand next Where to stop after running
8
>>> print(g.send(10))
s: 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration边栏推荐
猜你喜欢

DBeaver 安装及配置离线驱动

图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译

ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现

Use of better scroll

2.22.2.14
![[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching](/img/e4/27611abdd000019e70f4447265808c.jpg)
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching

Multipass中文文档-远程使用Multipass

0622-马棕榈跌9%

YOLOV5超参数设置与数据增强解析

钟珊珊:被爆锤后的工程师会起飞|OneFlow U
随机推荐
Laravel pay payment access process
2.8 learning summary
Comment enregistrer une image dans une applet Wechat
Genius makers: lone Rangers, technology giants and AI | ten years of the rise of in-depth learning
Solution to back-off restarting failed container
MySql如何删除所有多余的重复数据
Jenkins introduces custom jars
PHP get mobile number operator
dijkstra
LeetCode 94. Middle order traversal of binary tree
微信小程序保存圖片的方法
JWT token authentication verification
超高精度定位系统中的UWB是什么
mysql高级学习(跟着尚硅谷老师周阳学习)
Selection of programming language
NVM installation and use and NPM package installation failure record
天才制造者:独行侠、科技巨头和AI|深度学习崛起十年
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
2.9 learning summary
Rsync common error messages (common errors on the window)