Q:

Python: generator seems to always give me first element?

code
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

And I call the generator function like below:
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)

It shows only first element... Why does it happen?

A:

You are creating a new generator each time, perhaps you meant:
In []:
g = a.get_new_datetime()
next(g)

Out[]:
datetime.datetime(2000, 1, 1, 0, 0)

In []:
next(g)

Out[]:
datetime.datetime(2000, 1, 2, 0, 0)

Etc.

