Generator expression in class not producing output I expect
I'd like to use some generator expressions as attributes for some
expensive to compute data. If possible I would like to be able to do
something like this:
class Test:
def __init__(self):
self.data = []
self.evens = (i for i in self.data if i % 2 == 0)
def __main__():
t = Test()
t.data = [1,2,3,4,5,6,7,8]
for item in t.evens:
print(item)
if __name__ == '__main__':
__main__()
I would liked to have seen this print out 2 4 6 8 but this gives no
output. Have I made some sort of simple mistake here?
Is what I am trying to do here possible or will I need to use yeild to do
this?
No comments:
Post a Comment