Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad Python style to return empty iterators rather than None? [closed]

Opinions seem to be mixed on this -- is there a Pythonic "right way" to do this?

like image 532
espeed Avatar asked Feb 01 '13 03:02

espeed


Video Answer


1 Answers

I think empty iterator is better because iterating over nothing is faster then first checking if returned value is not None and then iterate over or not.

for x in function():
    do_something()

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

Just look at this.

Also normally you dont initialize iter attributes with None, but with empty iter object.

self.list_of_users = []

not

self.list_of_users = None
like image 185
Rafał Łużyński Avatar answered Oct 17 '22 06:10

Rafał Łużyński