我在同一个类中实现了一些逻辑过程。一个类实例为每个进程获取一个生成器,并run()
推进所述生成器。在我的情况下,发电机不会结束。
您将如何在下面的代码中调用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