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

r rt8396 at gmail.com
Thu Nov 27 10:48:08 EST 2008


On Nov 27, 8:57 am, Roy Smith <r... at panix.com> wrote:
> In article <mailman.4580.1227785954.3487.python-l... at python.org>,
>  "adam carr" <adambrianc... 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.

see this wiki for better understanding of Roy's suggestion...words to
live by my friend  =D
http://en.wikipedia.org/wiki/KISS_principle




More information about the Python-list mailing list