[Python-ideas] bool(datetime.time(0, 0))
M.-A. Lemburg
mal at egenix.com
Tue May 8 12:34:32 CEST 2012
Nick Coghlan wrote:
> On Tue, May 8, 2012 at 3:42 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Tue, May 08, 2012 at 01:57:13PM +1000, Nick Coghlan wrote:
>>> On Tue, May 8, 2012 at 12:57 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>>>> The behavior is broken. Midnight is not False.
>>>
>>> Whereas I disagree - I see having zero hour be false as perfectly
>>> reasonable behaviour (not necessarily *useful*, but then having all
>>> time objects report as True in boolean context isn't particularly
>>> useful either).
>>
>> On the contrary, it can be very useful to have all objects of some
>> classes treated as true. For example, we can write:
>>
>> mo = re.search(a, b)
>> if mo:
>> do_something_with(mo)
>>
>> without having to worry about the case where a valid MatchObject happens
>> to be false.
>>
>> Consider:
>>
>> t = get_job_start_time() # returns a datetime.time object, or None
>> if t:
>> do_something_with(t)
>>
>>
>> Oops, we have a bug. If the job happens to have started at exactly
>> midnight, it will wrongly be treated as false.
>
> IMO, you've completely misdiagnosed the source of that bug. Never
> *ever* rely on boolean evaluation when testing against None. *Always*
> use the "is not None" trailer.
Fully agreed.
The above code is just plain wrong and often causes problems
in larger applications - besides, it's also slower in most
cases, esp. if determining the length of an object or
converting it to a numeric value is slow.
If you want to test for None return values, you need to use
"if is None" or "if is not None".
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, May 08 2012)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2012-07-02: EuroPython 2012, Florence, Italy 55 days to go
2012-04-26: Released mxODBC 3.1.2 http://egenix.com/go28
2012-04-25: Released eGenix mx Base 3.2.4 http://egenix.com/go27
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list