我在同一个类中实现了几个逻辑进程。 一个类实例为每个进程获取一个生成器,并且run()
推进所述生成器。在我的情况下,发电机不会结束。Python生成器函数/对象命名约定
你怎么会在下面
调用 foo_function和 foo_object代码class C(threading.Thread):
def foo_function(self):
""" generator *function*,
logical process foo """
while True:
# some state checks
if self.some_attr:
# side-effects here
pass
yield
def __init__(self):
# generator *object*
# i.e. process instance
self.foo_object = self.foo_function() # <- here
def run(self):
while True:
next(self.foo_object)
next(self.another_object)
if xxx:
next(self.yet_another_object)
的典型方法discovery
,authentication
,watchdog
等
我如何命名功能定义生成器并以合理的方式包含生成器对象的属性?
最后只是为了踢,同名的名字将是疯了,对吧?
class C:
def foo(self):
yield 1; yield 2
def __init__(self):
self.foo = self.foo()
c = C()
type(C.foo) is function
type(c.foo) is generator
小伙子,你怎么样发表评论你认为是基于看法?毕竟它处理http://martinfowler.com/bliki/TwoHardThings.html :) – 2014-11-10 12:46:17
正如书面,是的这是非常混乱。 :)你有一些具体的细节可能有助于澄清这个非常抽象的例子吗?详细信息:foo()纯粹是为了副作用而运行? self.maybe_foo = foo_if_some_attr() – spazm 2014-11-18 08:01:42
你真的失去了我在这一行:self.name_me_process_generator = self.name_me_process_function() – spazm 2014-11-18 08:02:50