当前位置: 首页 > news >正文

广州市网站建设制作费用正在跳转页面

广州市网站建设制作费用,正在跳转页面,app图片怎么制作,app推广代理首先,假设大家都对于pytyhon的List comprehension的使用有了一定经验(它可以用于list,set,和dict哦) 不熟悉的参考介绍: Comprehending Python’s Comprehensions – dbader.org generator generator是哦…

首先,假设大家都对于pytyhon的List comprehension的使用有了一定经验(它可以用于list,set,和dict哦)

不熟悉的参考介绍: Comprehending Python’s Comprehensions – dbader.org

generator

generator是哦嗯一种返回lazy iterator的特殊类型函数。list comprehensions return full lists, while generator expressions return generators.

如下面代码,分别使用list和generator的构造,但是两者所占据的内存大家却完全不一样。

>>> import sys
>>> nums_squared_lc = [i ** 2 for i in range(10000)]
>>> sys.getsizeof(nums_squared_lc)
87624
>>> nums_squared_gc = (i ** 2 for i in range(10000))
>>> print(sys.getsizeof(nums_squared_gc))
120

 generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.

在python中,通过“类似list comprehension”或者yield的使用,我们可以构造generator并调用。

yield

yield的作用介绍如下,当使用generator的特殊方法,比如next()时,generator中的代码会被执行到yield,当经过yield时,python中generator的执行会被中断,函数的当前状态会被保存,直到下一次执行。 

下列函数是yield的一个经典用法。

def infinite_sequence():num = 0while True:yield numnum += 1

在调用该函数的过程中,每一次,输出的数会增加1,相比于return,yield会保存function当前的状态,参考How to Use Generators and yield in Python – Real Python

On the whole, yield is a fairly simple statement. Its primary job is to control the flow of a generator function in a way that’s similar to return statements. As briefly mentioned above, though, the Python yield statement has a few tricks up its sleeve.

When you call a generator function or use a generator expression, you return a special iterator called a generator. You can assign this generator to a variable in order to use it. When you call special methods on the generator, such as next(), the code within the function is executed up to yield.

When the Python yield statement is hit, the program suspends function execution and returns the yielded value to the caller. (In contrast, return stops function execution completely.) When a function is suspended, the state of that function is saved. This includes any variable bindings local to the generator, the instruction pointer, the internal stack, and any exception handling.

This allows you to resume function execution whenever you call one of the generator’s methods. In this way, all function evaluation picks back up right after yield. You can see this in action by using multiple Python yield statements:

 

 When you use next(), Python calls .__next__() on the function you pass in as a parameter. There are some special effects that this parameterization allows, but it goes beyond the scope of this article. Experiment with changing the parameter you pass to next() and see what happens!

http://www.yayakq.cn/news/409406/

相关文章:

  • 美发网站怎么做齐齐哈尔住房和城乡建设局网站
  • 制作一个公司网站用vs怎么做湘潭自适应网站建设 磐石网络
  • 安徽白云集团网站建设分销渠道系统
  • 企业网站模板下载哪家口碑好论文收录网站排名
  • 商标与logo的区别seo网站排名优化公司
  • 网站建设在哪里找人培训课程有哪些
  • 网站开发技术要求耀华建设管理有限公司网站
  • 做网站就是做点击率佛山新网站建设哪家好
  • 三九集团如何进行网站建设苏州网页设计app
  • 网站构建的过程软件开发公司服务
  • 济南免费网站建站模板负责网站建设和网络推广的
  • 网站建设大概要多少钱广州建筑工程公司名单
  • 做网站域名服务器千锋教育培训怎么样
  • 台州专业做网站做公司网站的总结
  • 免费分类信息网站大全照片书哪个网站做的好
  • 新干做网站如何用vs2012做网站
  • 网站开发岗位群海南省建设网站
  • 婚纱摄影网站建设广州网络科技有限公司
  • 网站遭到攻击绍兴专门做网站
  • 加工平台网站舒肤佳网络营销方案
  • iis如何做网站管理器乌镇网站建设投标书
  • 做英语手抄报 什么网站网站图片倒计时怎么做的
  • 网站设计要多久东莞百度代做网站联系方式
  • 苏州网站建设网站制作的公司网站建设的网站分析怎么写
  • 网站整合建设是啥意思营销软件app
  • 网站设计规划的目的和要求个人网站建设挂载下载链接
  • 青海设计网站如何做介绍监控公司的网站
  • 网站建设样本php网站开发程序编译软件
  • 小型网站项目策划书优质的聊城做网站
  • 域名不变修改网站怎么做wordpress输出响应式图片大小