Tag: 产量

有没有更好的方法来防止“收益”时,没有通过块?

我有一个方法,产量,看起来像: def a_method(*params) # do something yield # do something else end 如果一个块被传入,我希望这个方法产生块; 如果没有块被传入,那么该方法应该略微跳过产量信号,而不会像以下那样崩溃: no block given (yield) (LocalJumpError) 当然,最简单的方法是将方法改为: def a_method(*params, &block) # do something yield if block # do something else end 但是有没有更漂亮的方式?

Python中yieldexpression式的结果是什么?

我知道yield会将函数转换为生成器,但yieldexpression式本身的返回值是多less? 例如: def whizbang(): for i in range(10): x = yield i 这个函数执行的variablesx的值是什么? 我已经阅读了Python文档: http : //docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt ,似乎没有提到yieldexpression式本身的价值。

有些有助于理解“收益”

在我永恒的追求吮less我试图理解“收益”的声明,但我不断遇到同样的错误。 [someMethod]的主体不能是迭代器块,因为“System.Collections.Generic.List <AClass>”不是迭代器接口types。 这是我卡住的代码: foreach (XElement header in headersXml.Root.Elements()){ yield return (ParseHeader(header)); } 我究竟做错了什么? 我不能在迭代器中使用yield吗? 那有什么意义呢? 在这个例子中,它表示List<ProductMixHeader>不是一个迭代器接口types。 ProductMixHeader是一个自定义的类,但我想象List是一个迭代器接口types,不是? – 编辑 – 感谢所有的快速答案。 我知道这个问题并不是全新的,同样的资源不断涌现。 事实certificate,我想我可以返回List<AClass>作为返回types,但由于List<T>不懒,它不能。 改变我的返回types为IEnumerable<T>解决了这个问题:D 一个有点相关的问题(不值得打开一个新的线程):是否值得给IEnumerable<T>作为返回types,如果我确定99%的情况下我要去.ToList()呢? 性能影响是什么?

在Python中产生突破

根据这个问题的答案,C#中的yield break相当于在python中返回。 在正常情况下,“返回”确实停止了发电机。 但是,如果你的函数什么都不做,只能返回,你将得到一个None不是一个空的迭代器,它是通过在C# def generate_nothing(): return for i in generate_nothing(): print i 你会得到一个TypeError:'NoneType'对象是不可迭代的。 但是如果我在返回之前添加一个永不退出的yield,这个函数返回我所期望的。 def generate_nothing(): if False: yield None return 如果工作,但似乎有线。 谁有更好的主意? 谢谢,

C#:foreach中的yield返回失败 – body不能是一个迭代器块

考虑这一点混淆的代码。 其目的是通过匿名构造函数即时创build一个新对象,并yield return它。 目标是避免维护一个本地集合只是为了简单地return它。 public static List<DesktopComputer> BuildComputerAssets() { List<string> idTags = GetComputerIdTags(); foreach (var pcTag in idTags) { yield return new DesktopComputer() {AssetTag= pcTag , Description = "PC " + pcTag , AcquireDate = DateTime.Now }; } } 不幸的是,这段代码产生了一个exception: 错误28“Foo.BuildComputerAssets()”的主体不能是迭代器块,因为“System.Collections.Generic.List”不是迭代器接口types 问题 这个错误信息是什么意思? 我怎样才能避免这个错误,并正确使用yield return ?

我怎么知道一台发电机是否刚起步?

我想要一个函数, is_just_started ,其行为如下: >>> def gen(): yield 0; yield 1 >>> a = gen() >>> is_just_started(a) True >>> next(a) 0 >>> is_just_started(a) False >>> next(a) 1 >>> is_just_started(a) False >>> next(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> is_just_started(a) False 我怎样才能实现这个function? 我看着.gi_running属性,但似乎用于其他的东西。 如果我知道需要发送到生成器的第一个值,我可以这样做: def safe_send(gen, a): try: return gen.send(a) […]

产生一个recursion函数

我正在尝试对给定path下的所有文件进行操作。 我不想事先收集所有的文件名,然后用它们做一些事情,所以我试着这样做: import os import stat def explore(p): s = '' list = os.listdir(p) for a in list: path = p + '/' + a stat_info = os.lstat(path ) if stat.S_ISDIR(stat_info.st_mode): explore(path) else: yield path if __name__ == "__main__": for x in explore('.'): print '–>', x 但是这个代码跳过目录时,而不是让他们的内容。 我究竟做错了什么?

对于y()中的x:这是如何工作的?

我正在寻找代码在terminal中旋转光标,并发现这一点。 我想知道代码中发生了什么。 特别是for c in spinning_cursor():我从来没有见过这种语法。 是因为我一次从产生器返回一个元素,并且这个元素被分配给c? 在y()中使用x的这个任何其他的例子? import sys import time def spinning_cursor(): cursor='/-\|' i = 0 while 1: yield cursor[i] i = (i + 1) % len(cursor) for c in spinning_cursor(): sys.stdout.write(c) sys.stdout.flush() time.sleep(0.1) sys.stdout.write('\b')

Rails 3:yield / content_for有一些默认值?

有没有什么办法可以检测#content_for是否实际应用于Rails中的yield范围? 一个典型的例子就是: <title><%= yield :page_title %></title> 如果一个模板没有设置 <% content_for :page_title, "Something here" %> 有没有办法让布局把其他的东西呢? 我试图在布局中使用#content_for来定义它,但是这只会导致文本翻倍。 我也试过: <%= (yield :page_title) or default_page_title %> 其中#default_page_title是一个视图助手。 这只是把块完全留空。

Ruby:Proc#调用vs yield

Ruby的三种方法在以下两种实现之间的行为差​​异是什么? module WithYield def self.thrice 3.times { yield } # yield to the implicit block argument end end module WithProcCall def self.thrice(&block) # & converts implicit block to an explicit, named Proc 3.times { block.call } # invoke Proc#call end end WithYield::thrice { puts "Hello world" } WithProcCall::thrice { puts "Hello world" } “行为差异”包括error handling,性能,工具支持等。