代码
class MyClass:
def __init__(self):
from datetime import datetime
self.union_date = [datetime(2000,1,1), datetime(2000,1,2), datetime(2000,1,3)]
def get_new_datetime(self):
for date_time in self.union_date:
yield date_time
然后调用生成器函数,如下所示:
In [10]: a = MyClass()
In [11]: a.get_new_datetime()
Out[11]: <generator object MyClass.get_new_datetime at 0x7fa90bc20150>
In [12]: next(a.get_new_datetime())
Out[12]: datetime.datetime(2000, 1, 1, 0, 0)
In [13]: next(a.get_new_datetime())
Out[13]: datetime.datetime(2000, 1, 1, 0, 0)
In [14]: next(a.get_new_datetime())
Out[14]: datetime.datetime(2000, 1, 1, 0, 0)
它仅显示第一个元素...为什么会发生?
答案 0 :(得分:3)
您每次都在创建一个新的生成器,也许您的意思是:
IFS=",";
for port in $PORTS
do
$REDIS_HOME/bin/redis-server $REDIS_HOME/redis-$port.conf
done
等等。