On Wed 27 Jan 2010 11:13:53 AM EST, Oleg Broytman wrote:
> On Wed, Jan 27, 2010 at 03:30:18PM +0000, Matthew Wilson wrote:
>> def __nonzero__(self):
>> return self.count()
>
> This could led to a potential problem in code like this:
>
> if some_condition:
> query = MyTable.select(...)
> else:
> query = None
>
> if query:
> ...
>
> There is a subtle bug if self.count() returns 0. Currently people are
> accustomed that SelectResults instances are always evaluated to True in a
> boolean context.
> I don't know, though, how often people write code like that.
Yeah, there's certainly a downside of what I'm suggesting. The scenario
you describe doesn't worry me personally because I don't usually do
stuff like that, but I wouldn't want to break other people's code.
Here's another solution -- instead of defining nonzero, what about
defining __len__, so I could use this approach on both lists AND
selectResults objects:
if len(query):
...
The goal for me is to not need to remember if my collection is just a
query or whether it is a list of objects retrieved from a query.
Matt
|