Is Python style good for returning empty iterators rather than None?

Opinions seem to be mixed on this issue - do they have a pythonic "right way"?

+5
source share
2 answers

I think an empty iterator is better, because iterating over a niche is faster than the first check if the return value is not None, and then iterating or not.

for x in function():
    do_something()

value = function()
if value is not None:
    for x in value:
        do_something()

Just look at that.

Also, usually you do not initialize iter attributes with None, but with an empty iter object.

self.list_of_users = []

not

self.list_of_users = None
+7
source

, , , . , . , , .

, None, . , null , .

+5

All Articles