Live data from Hacker News

Please reconsider the Boolean evaluation of midnight

mail.python.org

151–160 of 218 posts

Re: Please reconsider the Boolean evaluation of midnight

#151

Earlier quoted context omitted.

Indeed, the first thing that came into my mind when reading this was "You wouldn't have this problem if the only valid type in a conditional was boolean."

The first thing that came into my mind when reading this was "You are a fool of a great intelligence."

That latter's debatable but probably not the former unfortunately.

Re: Please reconsider the Boolean evaluation of midnight

#152
post #115

INADA Naoki's argument [1] is succinct and insightful. I feel zero value of non abelian group should not mean False in bool context. () + () == () "" + "" == "" 0 + 0 == 0 timedelta() + timedelta() == timedelta() time() + time() => TypeError [1] https://mail.python.org/pipermail/python-ideas/2014-March/02...

Came here to say this. It's the most useful statement in the entire thread of "you're wrong!" "no, you're wrong!" and nobody noticed.

Perhaps people ignored it because it isn't true? {}+{} fails, because addition of dictionaries is non-abelian and Python refuses to choose a preferred result. But bool({}) is perfectly reasonable.

Re: Please reconsider the Boolean evaluation of midnight

#153

INADA Naoki's argument [1] is succinct and insightful. I feel zero value of non abelian group should not mean False in bool context. () + () == () "" + "" == "" 0 + 0 == 0 timedelta() + timedelta() == timedelta() time() + time() => TypeError [1] https://mail.python.org/pipermail/python-ideas/2014-March/02...

This is a nice clean criterion, but it's still pretty unclear to me why the zero element of a monoid should be considered falsey at all.

Because when writing a recursive function, the zero element is generally the base case.

    if x:
        
    else:
        return 
This applies to (natural) numbers as well--'destructuring' usually means decrementing the number.

Re: Please reconsider the Boolean evaluation of midnight

#154
post #150

INADA Naoki's argument [1] is succinct and insightful. I feel zero value of non abelian group should not mean False in bool context. () + () == () "" + "" == "" 0 + 0 == 0 timedelta() + timedelta() == timedelta() time() + time() => TypeError [1] https://mail.python.org/pipermail/python-ideas/2014-March/02...

If that were true, it would mean that dict should not support bool: >>> {} + {} Traceback (most recent call last): File " ", line 1, in TypeError: unsupported operand type(s) for +: 'dict' and 'dict' >>> bool({}) False Since I find bool(a_dict) to be very useful, and the lack of a_dict+b_dict to be reasonable (precisely because it's non-abelian!), I conclude that logic is cute, but not that relevant. Those who think…

{1: ["A", "B"]}?

Re: Please reconsider the Boolean evaluation of midnight

#155

I think the interesting part is what is revealed about Python and the difference with something like Ruby. Python is stable[0] and places a high degree of importance on backwards compatibility. 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. On the other hand Ruby (and most Ruby libraries) seem bo…

I'm gonna disagree with you! Well, I actually agree about ruby libraries , but the ruby standard library is the proper comparison here, which in my experience manages backwards compatibility and documentation of edge cases rather nicely. The (many) changes to the language itself since 1.8 have nearly all happened in backwards-compatible ways. So maybe the ruby community has a more flexible attitude (I've honestly nev…

I was deliberately discussing the whole library ecosystem you could well be right about the stability of the core of Ruby.

Re: Please reconsider the Boolean evaluation of midnight

#156
post #154
post #150

Earlier quoted context omitted.

If that were true, it would mean that dict should not support bool: >>> {} + {} Traceback (most recent call last): File " ", line 1, in TypeError: unsupported operand type(s) for +: 'dict' and 'dict' >>> bool({}) False Since I find bool(a_dict) to be very useful, and the lack of a_dict+b_dict to be reasonable (precisely because it's non-abelian!), I conclude that logic is cute, but not that relevant. Those who think…

{1: ["A", "B"]}?

Very little code that expects "A" or "B" would be equipped to handle ["A","B"].

Re: Please reconsider the Boolean evaluation of midnight

#157
post #154
post #150

Earlier quoted context omitted.

If that were true, it would mean that dict should not support bool: >>> {} + {} Traceback (most recent call last): File " ", line 1, in TypeError: unsupported operand type(s) for +: 'dict' and 'dict' >>> bool({}) False Since I find bool(a_dict) to be very useful, and the lack of a_dict+b_dict to be reasonable (precisely because it's non-abelian!), I conclude that logic is cute, but not that relevant. Those who think…

{1: ["A", "B"]}?

Okay, then what about

  {1: "A"} + {}
Should that return {1: ["A"]}? In which case {} is no longer a zero value.

What about

  {1: "A"} + {} + {1: "B", 2: "Z"} + {1: "C"} + {}

In any case, your suggestion would break all existing code. Based on all the StackOverflow and python-list questions, most people expect one or the other value, and few want the two-element list containing both. (And why a list instead of a tuple?)

Guido chose the current behavior because there is no clear-cut winner. "Refuse the temptation to guess."

Re: Please reconsider the Boolean evaluation of midnight

#158
Midnight UTC is zero's all the way down. Seems false to me, but I'm from the land of C. This seems to be in line with some low level hardware or common assembly practice across many languages.

Everyone is talking higher echelons of consideration, but what effect is there on generated byte code or in fitting within the virtual machine's tight pants?

Re: Please reconsider the Boolean evaluation of midnight

#159
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…

You can't document your way out of a usability problem.

This is a nice way of putting things. There's a school of thought that says if things work how it says somewhere that they should work, then no part of the system is exhibiting a problem. But just in this thread we can see Python's insane treatment of default function parameters called a "bug in the language". I've long been mad about what I can only think of as a bug in the Java spec: bit shifts use the base-32 modulus of their actual operand. Sure, the specification says that that's what they're supposed to do, but when I ask the JVM to fill a 32-bit value with 40 zeroes from the right, I expect one of the only two sane results -- an exception or whatever value 32 zero bits represents. I'm pretty sure there is no circumstance where shifting in 8 zeroes could be correct or useful.

Re: Please reconsider the Boolean evaluation of midnight

#160

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. The rule, in its entirety, is this: - Booleans are falsy when false. - Numbers are falsy when zero. - Containers are falsy when empty. - None is always falsy. - No other type of v…

Making anything besides booleans "truthy" strikes me as asking for a lot of trouble for very little gain. For each of these cases, how hard is this to write: x == 0 isempty(x) x == nothing How much clearer is the intent of that code than a truthy boolean test would be? This clarity is all the more important in dynamically typed languages without type annotations since the code itself doesn't give any hint what the ty…

Completely agree. C# has this right. An expression must produce a valid boolean value, the language does not evaluate any random type as boolean based on a set of rules.

E.g. you can't say following in C#

if(number) //number is of type integer.

if(o1) //o1 is a reference type variable.

if("string")

if(number = 10) // number = 10 is an assignment and produces 10.

You must write:

if(number == 0)

if(o1 != null)

if("string".Length > 0)

if(number == 10)

Post reply on HN