I was curious if anyone knew the rationale behind making midnight False?
--> import datetime
--> midnight = datetime.time(0,0,0)
--> bool(midnight)
False
To my way of thinking, midnight does actually exist so it should be
true. If datetime.time was measuring an *amount* of time, rather than a
certain point during the day, then a time of 0:0:0 should certainly be
False as it would mean no time had passed. However, since midnight does
indeed exist (as many programmers have observed when deadlines approach
;) I would think it should be true.
--
~Ethan~
Ethan,
Knights are true and seek the light. Evil trolls seek the night and so
their hour is false.
;-)
- Paddy.
> Ethan,
> Knights are true and seek the light. Evil trolls seek the night and so
> their hour is false.
>
> ;-)
That's speciest *and* lightist. There's nothing wrong with avoiding the evil
burning day star, that's practically de rigour for programmers.
*wink*
--
Steven
> Ethan Furman wrote:
>> Greetings, List!
>> I was curious if anyone knew the rationale behind making midnight
>> False?
>> --> import datetime
>> --> midnight = datetime.time(0,0,0)
>> --> bool(midnight)
>> False
>> To my way of thinking, midnight does actually exist so it should be
>> true. If datetime.time was measuring an *amount* of time, rather than
>> a certain point during the day, then a time of 0:0:0 should certainly
>> be False as it would mean no time had passed. However, since midnight
>> does indeed exist (as many programmers have observed when deadlines
>> approach ;) I would think it should be true.
>>
> I think it's because midnight is to the time of day what zero is to
> integers, or an empty string is to strings, or an empty container ...
So chr(0) should be False too...
--
Gabriel Genellina
> --> midnight = datetime.time(0,0,0)
> --> bool(midnight)
> False
I'd call this a bug.
Mark
...although looking at the source (see the function
time_nonzero in Modules/datetimemodule.c), this
behaviour is clearly intentional. So it's not a bug;
merely a questionable design decision. :)
Mark
One might ask the rationale behind treating a time as Boolean in the
first place. Let me guess: you are retrieving the times from a database,
and you are testing of the NULL value?
If that's the case, a simple "is not None" will overcome the objection.
Though I do agree it's a little weird for bool(midnight) to return
False. Probably worth a bug report. I imagine it's just returning the
bool of the underlying integer.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
>>> chr(0)
'\x00'
That's not an empty string. This is like...
>>> bool([0])
True
Now if Python had a char type one might expect the zero char to be
False but a collection of anything, even if the elements are False, is
not empty and hence is True.
As someone else said, there's not much point in casting time to boolean
but if you do, it is a base type, not a sequence so I wouldn't expect
to apply sequence logic to the outcome.
--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
No more so than zero being false. Zero exists too (check my bank
balance). Once you've accepted non-Boolean types having Boolean
values, the logic of what value they have is always going to be a bit
hairy. If you're unsure of the logic, just test against a value or a
range. After all, what value do you *expect* a time of day to have
when interpreted as a Boolean? If you don't have an expectation, why
are you interpreting it as a Boolean?
--
Tim Rowe
And, as Halmos said:
A box that contains a hat and nothing else is not the same thing as
a hat....
:)
--
Don't be afraid to ask (Lf.((Lx.xx) (Lr.f(rr)))).
So partly because of dates and datetimes, partly because objects are
true unless special effort is made to have them be false, and partly
because midnight is in fact a time of day, and not a lack of a time of
day, I do indeed expect it to be True.
--
~Ethan~
While it's not a lack of `time of day', it /is/ a lack of /elapsed/
time in the day ;)
Just as if you were using a plain integer or float to count the time :)