for loop - Python - Generator case where nothing to return -
i have generator so:
def iterate_my_objects_if_something(self): x in self.my_objects: if x.something: yield x
which call so:
for x in self.iterate_my_objects_if_something(): pass
in case there nothing return, tries iterate on nonetype , throws exception.
how return empty generator instead?
just simple check:
def iterate_my_objects_if_something(self): if self.my_objects: x in self.my_objects: if x.something: yield x
Comments
Post a Comment