iterating over a variable which could be None, a single object, or a list

Roy Smith roy at panix.com
Thu Nov 27 09:57:54 EST 2008


In article <mailman.4580.1227785954.3487.python-list at python.org>,
 "adam carr" <adambriancarr at gmail.com> wrote:

> I call a function get_items() which returns a list of items.
> However, in some cases, it returns just one item.
> It returns the item as an object though, not as a list containing one object.
> In other cases it simply returns None.

Almost certainly what you need to do is change the function to always 
return a list.  Might be a list of one object.  Might be an empty list.  
But always a list.  Keep the interface simple and obvious and you avoid 
complexities such as the one you're facing now.



More information about the Python-list mailing list