当前位置:网站首页>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边栏推荐
猜你喜欢

mysql高级学习(跟着尚硅谷老师周阳学习)

Dbeaver installation and configuration of offline driver

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

1.17 learning summary

0622 horse palm fell 9%

Sixtool- source code of multi-functional and all in one generation hanging assistant

How to use the configured slave data source for the scheduled task configuration class scheduleconfig

1.21 learning summary

08_ Spingboot integrated redis

2020-12-18
随机推荐
微信小程序保存图片的方法
PSIM software learning ---08 call of C program block
numpy 通用函数
Simple use of redis in laravel
How to carry out word-of-mouth marketing for enterprises' products and services? Can word of mouth marketing be done on behalf of others?
图解OneFlow的学习率调整策略
[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions
天才制造者:独行侠、科技巨头和AI|深度学习崛起十年
NVM installation and use and NPM package installation failure record
Anti withdrawal test record
Method of saving pictures in wechat applet
Multipass Chinese documents - improve mount performance
mysql高级学习(跟着尚硅谷老师周阳学习)
Dbeaver installation and configuration of offline driver
Multipass Chinese document - remote use of multipass
条件查询
PHP installation SSH2 extension
Thinkphp6 using kindeditor
08_ Spingboot integrated redis
BACK-OFF RESTARTING FAILED CONTAINER 的解决方法