Live data from Hacker News

Please reconsider the Boolean evaluation of midnight

mail.python.org

101–110 of 218 posts

Re: Please reconsider the Boolean evaluation of midnight

#101
post #52

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't want to break backwards-compatibility with that class of applications that nobody should ever write. That philosophy, taken to its logical conclusion, results in everything being broken forever.

My understanding is that the responder is saying that "That test does not mean what you are intending it to mean, it means something else that we've documented. We won't make it mean what you would like it to take make your incorrect code work, at the expense of breaking code that is using it in the documented way". I started on the side of the original person but was persuaded round by the responder. "if x:" means s…

One of the most basic pages of python documentation [1] says this:

    Any object can be tested for truth value, for use in an if or while 
    condition or as operand of the Boolean operations below. 
    The following values are considered false:

        None

        False

        zero of any numeric type, for example, 0, 0L, 0.0, 0j.

        any empty sequence, for example, '', (), [].

        any empty mapping, for example, {}.

        instances of user-defined classes, if the class defines a __nonzero__() 
        or __len__() method, when that method returns the integer zero or bool value False. [1]

    All other values are considered true — so objects of many types are always true.
To me, that makes it clear that no valid time could be False. It isn't on that list, so it should be True. Is there any controversy about that?

[1] http://docs.python.org/2/library/stdtypes.html

Re: Please reconsider the Boolean evaluation of midnight

#102

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't want to break backwards-compatibility with that class of applications that nobody should ever write. That philosophy, taken to its logical conclusion, results in everything being broken forever.

Yes. This is where Linus would step in and say "SHUT THE FUCK UP! We don't break user space, EVER!" Developers who enter into the mindset that the users of their code are using it "wrong" are a blight and will always be a blight. Too bad Python has a BDFL and not a Raging DFL.

Linus does NOT break backwards compatibility. That is what this change implies.

Which doesn't mean I disagree with changing the behaviour, but I doubt Linus would use that reason for that.

Re: Please reconsider the Boolean evaluation of midnight

#103
post #86

Earlier quoted context omitted.

If you need to use a test like that, you've already gone too far down the road of lazy "falsey" value usage, and whoever wrote it didn't bother with the idea of sensible defaults or consistent typing/initialization. I agree that midnight evaluating to false is ridiculous (I wasn't aware of this, thankfully I've never run into it). I'm not sure where you got the idea that I support this, but I don't.

It's functionality that someone went out of their way to create: def __bool__(self): if self.second or self.microsecond: return True offset = self.utcoffset() or timedelta(0) return timedelta(hours=self.hour, minutes=self.minute) != offset http://hg.python.org/cpython/file/302c8fdb17e3/Lib/datetime.... Now that I look at the actual code, it makes less sense. It's not just midnight that evaluates to False. It's midnig…

Perhaps it started because as time zones were added, they wanted to reflect that 8am_pacific == midnight_utc, assuming there aren't any offsets for daylight savings in effect...

Re: Please reconsider the Boolean evaluation of midnight

#104

I've never seen a good argument for anything beside "false" to be considered false. Likewise for "true". Keystrokes are not a commodity for most coders, and compilers are not dumb; just be explicit and write "!= 0" or whatever. (And 0 == False, "" != False, but both 0 and "" are considered false? C'mon Python, that's borderline JavaScript territory.)

I sometimes think that way, but when I do ask I ask why you can use any non-Boolean in a conditional? That's optional too.

As for uses: because empty lists, zero and empty strings are false, you can do things like:

if validation_errors: if special_instructions: if unusued_widgets:

Of course those are just saved keystrokes, and can lead to bugs, but so is being able to throw a list into a condition.

Re: Please reconsider the Boolean evaluation of midnight

#106
post #52

Earlier quoted context omitted.

My understanding is that the responder is saying that "That test does not mean what you are intending it to mean, it means something else that we've documented. We won't make it mean what you would like it to take make your incorrect code work, at the expense of breaking code that is using it in the documented way". I started on the side of the original person but was persuaded round by the responder. "if x:" means s…

One of the most basic pages of python documentation [1] says this: Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false: None False zero of any numeric type, for example, 0, 0L, 0.0, 0j. any empty sequence, for example, '', (), []. any empty mapping, for example, {}. instances of user-defined classes, if…

For the purposes of that document, datetime is not a builtin type but a user-defined class (since it's written in a Python package and not the interpreter directly). It has a __nonzero__ method defined.

Re: Please reconsider the Boolean evaluation of midnight

#107
post #23

Earlier quoted context omitted.

Python has truthiness and falsiness as well. That shortens code like: if not x: do_something() x could be 0, None, False, empty set set(), {}, [], (), '',"" or an I believe any class or object that appropriately overrides __nonzero__() or __bool__() methods. It is a matter of taste, so I rather like it.

Well, if you read the mailing list thread there are several core python maintainers saying that is bad style. So I wouldn't call it just a matter of taste when core maintainers disagree with it and parts of the standard library break your code when you do it.

> re several core python maintainers saying that is bad style.

Well I am a python programmer that has been using it for almost 10 years, 7 full time, and I say it is nice. So yes, I would call it as a matter of taste.

Re: Please reconsider the Boolean evaluation of midnight

#108

Earlier quoted context omitted.

One of the most basic pages of python documentation [1] says this: Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false: None False zero of any numeric type, for example, 0, 0L, 0.0, 0j. any empty sequence, for example, '', (), []. any empty mapping, for example, {}. instances of user-defined classes, if…

For the purposes of that document, datetime is not a builtin type but a user-defined class (since it's written in a Python package and not the interpreter directly). It has a __nonzero__ method defined.

You're correct, it falls under that last rule of user-defined class. They certainly have a right to define it that way, in some sense.

But it just goes entirely against the spirit of that documentation to do so. I would be fairly shocked if there are many other examples of exceptions to this rule in the standard library. That's just not how it is supposed to work. I'm primarily a python developer, and I have that list of False things very deeply internalized. They are False, other things are True. I'm sure most others devs have as well.

It is right at the top of the page on the documentation of the standard types.

I'm sure there could other classes where the truthiness had some obvious physical meaning, and objects could be either True or False in a meaninful way. But midnight is not one of those.

Re: Please reconsider the Boolean evaluation of midnight

#109
post #54
post #35

Earlier quoted context omitted.

This is what is plaguing the ocaml compiler and standard library. Overconservatism with respect to obscure features. For example, a patch was written to speed up hashtables (IIRC), and was rejected because it would change the result of hashing format strings (type-safe format strings have a different type from plain strings). I can't imagine a single application where you would need to preserve the value of hashed fo…

It does sound like they might have gone a bit far, but as someone who works in an industrial context, a tool that maintains backwards compatibility rigorously is of enormous benefit. The case you might have to make to upgrade the version of the tool you are using has to take into account the risks, and something that is maintaining a high level of backwards compatibility has a much lower level of risk.

Here are my preferences for tools, in order from most-preferred to least preferred: first, tools that have a rigorous deprecation process for breaking changes, distant second, tools that are incredibly conservative about breaking changes, pretty close third, tools that refuse to make breaking changes at all ever, astronomically distant last, tools that make breaking changes willy-nilly.

I generally eventually have to re-write to use a totally different tool for either of the middle two cases, because they just can't keep up. I would much rather make changes to my use of them based on deprecations than be forced to ditch them entirely.

(This is more a general statement - obviously refusing to make this datetime change isn't realistically going to push anybody away from Python.)

Re: Please reconsider the Boolean evaluation of midnight

#110

So ignoring the hype, here's the outcome-to-date... The ticket was reconsidered, reopened and classified as a bug. http://bugs.python.org/msg212771 Nick Coghlan's dissection of the issue here: https://mail.python.org/pipermail/python-ideas/2014-March/02... is pretty much perfect - wonderful piece of technical writing! Donald Stufft has expressed an interest in making the patch for this happen, and assuming all goes a…

Neat! This stuff actually works sometimes!
Post reply on HN