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


------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to