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

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -