[Python-Dev] Iterating over objects of unknown length (original) (raw)

Phillip J. Eby pje at telecommunity.com
Wed Sep 26 18:33:33 CEST 2007


At 07:24 PM 9/26/2007 +0400, Oleg Broytmann wrote:

Hello!

(This seems like a "developing with Python" question and partially it is but please read on.) I have a class that represents SQL queries. Instances of the class can be iterated over. As an SQL query doesn't know in advance if it will produce any row the class doesn't implement len(). Moreover, users of the class sometimes write if sqlQuery: for row in sqlQuery: ... else: # no rows

This isn't consistent with iterators; e.g.:

x=iter([]) if x: print "yes" ... yes

ISTM that you should be returning "True" from nonzero, since you don't implement len().

1. Should I consider this a bug in the logging module (and other libraries) and submit patches? 2. Or should I stop raising exceptions in nonzero()?

#2 - Python objects should always be nonzero, unless they are empty containers, zeros, or otherwise specifically False. It's reasonable for libraries to expect that truth-testing an object is always safe.



More information about the Python-Dev mailing list