<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,Arial,'Hiragino Sans GB','Microsoft YaHei',simsun;vertical-align:baseline;color:rgb(93,93,93);background-color:rgb(255,255,255);">Python的三大神器:装饰器.迭代器与生成器!这就是Python的三大神器,好了废话不多说。直接来上干货吧!
<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">生成器<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">仅仅拥有生成某种东西的能力,如果不用next方法是获取不到值得。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">创建一个生成器函数<pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;border:0px;font-size:18px;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(93,255);"><pre class="source-code" style="margin-bottom:0px;padding:15px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;background:rgb(243,244,245);"><code style="margin:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(153,153,153);">>>> <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(149,65,33);">def <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(68,85,136);">scq<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(0,255);">():<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(153,153);">... print(<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(33,145,97);">"11")
<span style="margin:0px;padding:0px;border:0px;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(64,128,128);"># 当函数代码块中遇到yield关键字的时候,这个函数就是一个生成器函数
<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(153,153);">... <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(149,33);">yield <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(64,160,112);">1
<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(153,97);">"22")
<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(153,112);">2
<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(153,97);">"33")
<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(153,112);">3
...
<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">把生成器赋值给一个对象<pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;border:0px;font-size:18px;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(93,245);"><code style="margin:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">>>> r = sc<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(33,97);">q()
<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">查看r的苏剧类型并且输出r的值<pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;border:0px;font-size:18px;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(93,153);">>>> print(type(r),r)
<<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(149,33);">class '<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(68,136);">generator'> <generator <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(68,136);">object <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(68,136);">scq <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(68,136);">at 0<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;color:rgb(68,136);">x000001F117D8DF10>
<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">当执行生成器的next的时候,代码会按照顺序去执行,当执行到yield时会返回并提出,yield后面的值就是返回值,然后记录代码执行的位置,并退出<div class="pgc-img" style="margin:0px;padding:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p class="pgc-img-caption" style="margin-bottom:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。