草庐IT

yield-keyword

全部标签

python - 两个连续的 yield 语句如何在 python 中工作?

这个问题在这里已经有了答案:Whatdoesthe"yield"keyworddo?(51个答案)关闭上个月。我从pymotw.com中偶然发现了这段代码在合并和拆分部分。fromitertoolsimport*defmake_iterables_to_chain():yield[1,2,3]yield['a','b','c']foriinchain.from_iterable(make_iterables_to_chain()):print(i,end='')print()我不明白make_iterables_to_chain()是如何工作的。它包含两个yield语句,它是如何工作的

python - 当 Python yield 语句没有表达式时会发生什么?

我是一名试图理解一些Python代码的C#程序员。有问题的代码是一个生成器函数,看起来像这样:deffunc():oldValue=curValueyieldcurValue=oldValue如果我理解正确,这将生成一个包含一个成员的可迭代序列。但是,yield语句后没有表达式。这样一个没有表达式的语句应该产生什么?有没有使用这种编码方式的Python习语? 最佳答案 它将产生None;就像一个空的return表达式会:>>>deffunc():...yield...>>>f=func()>>>next(f)isNoneTrue您可

python - 将 yield 与多个 ndb.get_multi_async 一起使用

我正在尝试提高我当前从应用引擎数据存储区查询的效率。目前,我使用的是同步方法:classHospital(ndb.Model):name=ndb.StringProperty()buildings=ndb.KeyProperty(kind=Building,repeated=True)classBuilding(ndb.Model):name=ndb.StringProperty()rooms=ndb.KeyProperty(kind=Room,repeated=True)classRoom(ndb.Model):name=ndb.StringProperty()beds=ndb.Ke

python - 在 Python 中有类似于 Java 的 Thread.yield() 的东西吗?这有意义吗?

我想告诉我的Python线程让步,从而避免不必要地占用CPU。在Java中,您可以使用Thread.yield()函数来实现。我认为Python中没有类似的东西,所以我一直在使用time.sleep(t),其中t=0.00001。对于t=0似乎没有效果。我认为也许我对Python的线程模型有一些不正确的理解,因此缺少thread.yield()的原因。有人可以向我澄清这一点吗?谢谢!PS:Java的Thread.yield()的文档是这样说的:Causesthecurrentlyexecutingthreadobjecttotemporarilypauseandallowotherth

python 2 : different meaning of the 'in' keyword for sets and lists

考虑这个片段:classSomeClass(object):def__init__(self,someattribute="somevalue"):self.someattribute=someattributedef__eq__(self,other):returnself.someattribute==other.someattributedef__ne__(self,other):returnnotself.__eq__(other)list_of_objects=[SomeClass()]print(SomeClass()inlist_of_objects)set_of_obj

python - python 3.3.2+ 中的 yield from 和 yield 之间有什么区别

在python3.3.2+之后python支持创建生成器函数的新语法yieldfrom我已经通过快速尝试了这个>>>defg():...yieldfrom[1,2,3,4]...>>>foriing():...print(i)...1234>>>看似简单易用,但PEP文档很复杂。我的问题是,与之前的yield语句相比,还有其他区别吗?谢谢。 最佳答案 对于大多数应用程序,yieldfrom只是按顺序从左边的iterable开始生成所有内容:defiterable1():yield1yield2defiterable2():yield

python - 在另一个函数中为生成器调用 yield

假设我有一些经理对象。这个对象的API有一个main_hook函数,它获取另一个函数f作为它的参数,并在循环中运行给定的f,做一些事情在每次迭代之间:defmain_hook(self,f):while(self.shouldContinue()):#dosomepreparationsf(self)#dosometeardown现在,我还有(更准确地说,想要)一个函数stop_and_do_stuff,一旦调用,就会停止main_hook它跟踪,将控制权返回给名为main_hook的任何函数,在该函数完成其正在执行的操作后,将控制权返回给main_hook并继续。基本上结果会和做的一

python - tensorflow 错误 : TypeError: __init__() got an unexpected keyword argument 'dct_method'

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。Improvethisquestion运行TensorflowObjectDetectionAPI时在当前版本的Tensorflow-Gpu(1.5)中,尝试训练一组自定义图像时会抛出以下错误。INFO:tensorflow:Scaleof0disablesregularizer.INFO:

python - asyncio as_yielded 来自异步生成器

我希望能够从多个异步协程中产生结果。Asyncio的as_completed有点接近我正在寻找的东西(即我希望任何协程能够随时返回调用者然后继续),但这似乎只是允许常规协程返回一次。这是我目前所拥有的:importasyncioasyncdeftest(id_):print(f'{id_}sleeping')awaitasyncio.sleep(id_)returnid_asyncdeftest_gen(id_):count=0whileTrue:print(f'{id_}sleeping')awaitasyncio.sleep(id_)yieldid_count+=1ifcount>

Python 检查模块 : keyword only args

Python中的短语“仅关键字args”有点模棱两可——通常我认为它是指传递给**kwarg参数的args。但是,inspect模块似乎区分了**kwarg和所谓的“仅关键字参数”。来自thedocs:inspect.getfullargspec(func)GetthenamesanddefaultvaluesofaPythonfunction’sarguments.Anamedtupleisreturned:FullArgSpec(args,varargs,varkw,defaults,kwonlyargs,kwonlydefaults,annotations)argsisalist