TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Please reconsider the Boolean evaluation of midnight

337 pointsby rivertover 11 years ago

30 comments

tomchristieover 11 years ago
So ignoring the hype, here&#x27;s the outcome-to-date...<p>The ticket was reconsidered, reopened and classified as a bug. <a href="http://bugs.python.org/msg212771" rel="nofollow">http:&#x2F;&#x2F;bugs.python.org&#x2F;msg212771</a><p>Nick Coghlan&#x27;s dissection of the issue here: <a href="https://mail.python.org/pipermail/python-ideas/2014-March/026647.html" rel="nofollow">https:&#x2F;&#x2F;mail.python.org&#x2F;pipermail&#x2F;python-ideas&#x2F;2014-March&#x2F;02...</a> is pretty much perfect - wonderful piece of technical writing!<p>Donald Stufft has expressed an interest in making the patch for this happen, and <i>assuming</i> all goes as planned this usage will raise a deprecation warning in 3.5 and be fully fixed in 3.6.<p>News in brief: User raises issue. Issue gets resolved.
评论 #7355664 未加载
评论 #7359064 未加载
评论 #7365191 未加载
nbouscalover 11 years ago
If I understand the argument there correctly, the responder is saying: Nobody should ever use this functionality, instead they should always check that the date is not None. So, we should leave this broken, because we don&#x27;t want to break backwards-compatibility with that class of applications that nobody should ever write.<p>That philosophy, taken to its logical conclusion, results in everything being broken forever.
评论 #7354352 未加载
评论 #7355075 未加载
评论 #7354833 未加载
评论 #7354543 未加载
评论 #7354232 未加载
评论 #7355459 未加载
clarkevansover 11 years ago
INADA Naoki&#x27;s argument [1] is succinct and insightful.<p><pre><code> I feel zero value of non abelian group should not mean False in bool context. () + () == () &quot;&quot; + &quot;&quot; == &quot;&quot; 0 + 0 == 0 timedelta() + timedelta() == timedelta() time() + time() =&gt; TypeError </code></pre> [1] <a href="https://mail.python.org/pipermail/python-ideas/2014-March/026674.html" rel="nofollow">https:&#x2F;&#x2F;mail.python.org&#x2F;pipermail&#x2F;python-ideas&#x2F;2014-March&#x2F;02...</a>
评论 #7357823 未加载
评论 #7357295 未加载
评论 #7357526 未加载
评论 #7355762 未加载
评论 #7358403 未加载
评论 #7359061 未加载
评论 #7359947 未加载
评论 #7357456 未加载
评论 #7358964 未加载
colandermanover 11 years ago
I&#x27;ve never seen a good argument for anything beside &quot;false&quot; to be considered false. Likewise for &quot;true&quot;. Keystrokes are not a commodity for most coders, and compilers are not dumb; just be explicit and write &quot;!= 0&quot; or whatever.<p>(And 0 == False, &quot;&quot; != False, but both 0 and &quot;&quot; are considered false? C&#x27;mon Python, that&#x27;s borderline JavaScript territory.)
评论 #7355343 未加载
评论 #7354243 未加载
评论 #7356432 未加载
评论 #7354209 未加载
评论 #7354366 未加载
评论 #7354255 未加载
评论 #7355554 未加载
评论 #7354280 未加载
评论 #7354238 未加载
评论 #7354465 未加载
unotiover 11 years ago
I just got bit by this a few days ago. I was creating an event scheduling system that uses either repeating entries with a datetime.time, or one time entries with a datetime.datetime. I had code that said &quot;if start_time&quot; to see which it was, and discovered later that midnight evaluates to false. It&#x27;s not the best idea.
评论 #7359566 未加载
ggchappellover 11 years ago
Ignoring Python for a bit and thinking as a designer of some hypothetical future language: there is a nice rule given here for evaluation in a Boolean context. I wonder whether it should be taken as a general guideline for future languages.<p>The rule, in its entirety, is this:<p>- Booleans are falsy when false.<p>- Numbers are falsy when zero.<p>- Containers are falsy when empty.<p>- None is always falsy.<p>- No other type of value is ever falsy.<p>I can think of two ways we might possibly want to alter the rule.<p>The first is to expand the idea of <i>number</i> to include arbitrary groups (or monoids?), with the identity element being falsy. So, for example, a matrix with all entries zero might be falsy. Or a 3-D transformation might be falsy if it does not move anything.<p>The second is one I have encountered in C++. There, an I&#x2F;O stream is falsy if it is in an error state. This makes error checking easy; there is one less member-function name to remember. We might expand this idea to include things like Python&#x27;s urllib, or any object that wraps a connection or stream of some kind.<p>EDIT: OTOH, there is the Haskell philosophy, where the only thing that can be evaluated in a Boolean context is a Bool, so the only falsy thing is <i>False</i>.<p>EDIT 2: The comment by clarkevans (quoting a message from INADA Naoki) already partially addressed the above group idea: &quot;I feel zero value of non abelian group should not mean False in bool context.&quot;
评论 #7357512 未加载
评论 #7357539 未加载
hyperpapeover 11 years ago
James Coglan recently pointed out that all of Python&#x27;s falsy values are the additive identity of some type. Midnight fits the mold.<p>This results in some weird results from an intuitive perspective, but is very principled and elegant in other ways.<p>My one objection was that I don&#x27;t know how None fits in.
评论 #7354628 未加载
评论 #7354623 未加载
评论 #7354650 未加载
wzddover 11 years ago
Lots of Python objects are falsey: empty lists, empty strings, etc. So it&#x27;s never a good idea to write &quot;if &lt;thing&gt;&quot; when you mean &quot;if &lt;thing&gt; is not None&quot;.<p>This is pretty well-known, I thought.
评论 #7354154 未加载
评论 #7354210 未加载
dec0dedab0deover 11 years ago
Off the top of my head I can&#x27;t think of a reason to check if a date exists, but I would certainly expect midnight to be truthy if I found a reason.
评论 #7354030 未加载
评论 #7354028 未加载
Nanzikambeover 11 years ago
Whilst reading that thread, I stumbled accross:<p><pre><code> &quot;goto fail&quot; is a well-known error handling mechanism in open source software, widely reputed for its robusteness: http:&#x2F;&#x2F;opensource.apple.com&#x2F;source&#x2F;Security&#x2F;Security-55471&#x2F;libsecurity_ssl&#x2F;lib&#x2F;sslKeyExchange.c https:&#x2F;&#x2F;www.gitorious.org&#x2F;gnutls&#x2F;gnutls&#x2F;source&#x2F;6aa26f78150ccbdf0aec1878a41c17c41d358a3b:lib&#x2F;x509&#x2F;verify.c I believe Python needs to add support for this superior paradigm. It would involve a new keyword &quot;fail&quot; and some means of goto&#x27;ing to it. I suggest &quot;raise to fail&quot;: if (some_error): raise to fail fail: &lt;error handling code&gt; Unless there are many objections, this fantastic idea might be submitted in a (short) PEP somewhere around the beginning of next month. There is some obvious overlap with the rejected &quot;goto PEP&quot; (PEP 3163) and the Python 2.3 goto module. However, the superiority of goto fail as error generation and error handling paradigm has since then been thoroughly proven. </code></pre> <a href="https://mail.python.org/pipermail/python-ideas/2014-March/026581.html" rel="nofollow">https:&#x2F;&#x2F;mail.python.org&#x2F;pipermail&#x2F;python-ideas&#x2F;2014-March&#x2F;02...</a>
评论 #7354646 未加载
评论 #7354187 未加载
spacemanmattover 11 years ago
I think he understates the most powerful part of his argument.<p>Midnight is a value, not a special value. There is no reason why it or any other valid time should be falsey on a daily cycle.
评论 #7356491 未加载
josephlordover 11 years ago
I think the interesting part is what is revealed about Python and the difference with something like Ruby.<p>Python is stable[0] and places a high degree of importance on backwards compatibility.<p>This behaviour is well documented (and called out for particular note). This reinforces that it is (a) official and (b) not a bug because it is the documented behaviour.<p>On the other hand Ruby (and most Ruby libraries) seem both less concerned with backwards compatibility, have less thorough documentation[1] but are more willing to change and improve.<p>There isn&#x27;t a right and a wrong between these approaches although for most things I think I would prefer something between the two. I think I generally prefer Python in terms of syntax (Ruby is a bit too flexible with too many ways to do things for my taste) but I do wonder if Python will be left a little behind.<p>[0] Python 2&#x2F;3 transition is a single big deliberate change.<p>[1] I have an open Rails issue that I don&#x27;t know if is a bug or not because there isn&#x27;t documentation that is sufficient to compare the behaviour with so it is a case of what feels right&#x2F;wrong: <a href="https://github.com/rails/rails/issues/6659" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rails&#x2F;rails&#x2F;issues&#x2F;6659</a>
评论 #7355706 未加载
delinkaover 11 years ago
Not being a Pythonista, I have the following questions:<p>1) Is there a (native or custom) date type in Python? Is it an object?<p>2) Midnight <i>when</i>? Today? This date last year? Sure there&#x27;s a &quot;zero value&quot; for dates - it&#x27;s the epoch for whichever platform or library you&#x27;re using.<p>3) Why in would anyone call it a &quot;date&quot; if it&#x27;s really a <i>time</i>?<p>Maybe I&#x27;m getting off into the philosophical decisions of the reptile wranglers, but this particular debate sounds a lot like someone made a decision long ago that had ramifications further than expected and now the justification is engrained, things are built on it, and no one&#x27;s willing to make the &#x27;correction.&#x27;
评论 #7354365 未加载
评论 #7354685 未加载
评论 #7354750 未加载
boukover 11 years ago
Python has weird ideas about comparisons, I&#x27;m pretty sure it&#x27;s the only language where this is possible: <a href="https://eval.in/113749" rel="nofollow">https:&#x2F;&#x2F;eval.in&#x2F;113749</a>
评论 #7356001 未加载
评论 #7356012 未加载
eq-over 11 years ago
The only reason for midnight being a falsy value that I can think of is that someone thought that <i>all</i> objects should provide some functionality for __nonzero__&#x2F;__bool__.<p>It was a bad idea.
Idekaover 11 years ago
This kind of crap is exactly the reason why I don&#x27;t like doing just &quot;if var:&quot; unless var is guaranteed to be a boolean.
pistleover 11 years ago
Midnight UTC is zero&#x27;s all the way down. Seems false to me, but I&#x27;m from the land of C. This seems to be in line with some low level hardware or common assembly practice across many languages.<p>Everyone is talking higher echelons of consideration, but what effect is there on generated byte code or in fitting within the virtual machine&#x27;s tight pants?
mannykannotover 11 years ago
This offers a counterexample to the simplistic notion that &#x27;duck typing&#x27; results in programs that automagically do the right thing. The reality is that duck typing does not relieve you of the responsibility of understanding the semantics of the elements you use to construct a program from.
abvdaskerover 11 years ago
On the plus side, Boolean Value: Midnight would make a great CS-themed action movie title.
doolsover 11 years ago
This is freakishly similar to the discussion on a PHP bug I submitted in 2006:<p><a href="https://bugs.php.net/bug.php?id=39579" rel="nofollow">https:&#x2F;&#x2F;bugs.php.net&#x2F;bug.php?id=39579</a>
einhverfrover 11 years ago
This sub discussion seemed interesting to me: <a href="https://mail.python.org/pipermail/python-ideas/2014-March/026656.html" rel="nofollow">https:&#x2F;&#x2F;mail.python.org&#x2F;pipermail&#x2F;python-ideas&#x2F;2014-March&#x2F;02...</a><p>My first thought was this:<p><a href="http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements005.htm" rel="nofollow">http:&#x2F;&#x2F;docs.oracle.com&#x2F;cd&#x2F;B19306_01&#x2F;server.102&#x2F;b14200&#x2F;sql_el...</a>
nasover 11 years ago
While I agree this is surprising behavior and I wouldn&#x27;t design an API this way, it is documented behavior. From the docs:<p>&quot;in Boolean contexts, a time object is considered to be true if and only if, after converting it to minutes and subtracting utcoffset() (or 0 if that’s None), the result is non-zero&quot;<p>Changing at this point would possibly break code that relied on documented library behavior. That&#x27;s not a responsible thing to do.
评论 #7354141 未加载
评论 #7354152 未加载
评论 #7354418 未加载
mark-rover 11 years ago
In every other language I&#x27;ve used, a time value of 0 is used when a datetime only contains a date and doesn&#x27;t have a specific time. The existing behavior would make sense in that context. I know Python also has a separate date object, are the two interchangeable enough that you could mix and match without problems?
Robadobover 11 years ago
I came across a similar issue when using rails the other day, where I gave my model a boolean field that had presence validation. The presence validation of the boolean field fails if the bool is set to false, had me confused for a while, but It wasn&#x27;t a big enough issue for me to research&#x2F;report.
lutuspover 11 years ago
It seems there are two choices:<p>1. Before applying a numerical value to a Boolean test, ask whether it can ever be zero when that&#x27;s not the intent of the test.<p>2. Create a new rule that forbids testing numerical values as though they&#x27;re Booleans, and break nearly every program in existence.<p>Hmm ... wait ... I&#x27;m thinking it over.
njharmanover 11 years ago
Why would anyone evaluate dates in a boolean context? They are (should be) always True.
评论 #7357464 未加载
jfbover 11 years ago
Creeping semi-booleans make me very uncomfortable. But what&#x27;s the alternative? A-values and I-values? A &quot;μ&quot; for questions unanswerable in the type system? Just punt and let Javascriptisms take over the world?
joelthelionover 11 years ago
This is how languages die. I wasn&#x27;t aware that Python had become such a bureaucracy.<p>The current behavior is insane - just fix it! No need for days of discussion on the mailing list or three-point non regression plans.
murbard2over 11 years ago
Deprecate datetime and introduce datetime2 with better behavior for midnight. Problem solved.
platzover 11 years ago
boolean blindness <a href="http://existentialtype.wordpress.com/2011/03/15/boolean-blindness/" rel="nofollow">http:&#x2F;&#x2F;existentialtype.wordpress.com&#x2F;2011&#x2F;03&#x2F;15&#x2F;boolean-blin...</a>