News for hackers. Stuff that matters.
Re:The 2,500 year old history of why Python’s all([]) return... by jeremysalwen on Wednesday March 18, 2020 @05:28AM (#22614898)
Maybe it's because I studied mathematics, but to me the `all([])==True` seems so obviously true and logical, and that any other behavior would be extremely surprising, and unsafe, for the opposite reason.
Re:The 2,500 year old history of why Python’s all([]) return... by imtringued on Wednesday March 18, 2020 @08:27AM (#22615815)
It's pretty obvious from any standpoint.

all([True, True] + []) is equivalent to `True and True and ?`. If you replace ? with False then all() will never return True in any case because given any list you can always create an equivalent list by appending [] which would taint the list to always return False on all(). Obviously nobody wants all() to return False on a valid list and the concept of tainting a list is pretty stupid so True is the only option. If you replace ? with True you can add as many empty lists as you like which is the desired behavior.

all([True, True] + [] + [] + []) == True and True and True and True and True

Re:The 2,500 year old history of why Python’s all([]) return... by gridlockd on Wednesday March 18, 2020 @09:15AM (#22616251)
> It's pretty obvious from any standpoint.

If it was obvious from any standpoint, we obviously wouldn't be having this blog post or this discussion thread.

Natural language - the natural way to think about things - does not always intuitively translate over to predicate logic. If you disagree with that, you should try teaching it to a class of high schoolers.

Re:The 2,500 year old history of why Python’s all([]) return... by earthboundkid on Wednesday March 18, 2020 @10:20PM (#22623759)
Lol, I used to believe that logic was an intrinsic faculty of human reason, and then I taught a bunch of freshmen.
Re:The 2,500 year old history of why Python’s all([]) return... by gridlockd on Wednesday March 18, 2020 @05:42AM (#22614966)
It probably is because you studied mathematics.

If you think of "all" as "in this set, no element exists which is False" then it is "obviously" the right behavior.

However, consider this more practical example:

    x = {1,2,3}
    y = {}

    all(e in x for e in y)
    > True
This doesn't strike me as obviously right.
Re:The 2,500 year old history of why Python’s all([]) return... by yorwba on Wednesday March 18, 2020 @05:48AM (#22614994)
That's mathematically equivalent to "y is a subset of x", which is obviously right. (Except for the wrinkle that y is a dict, not a set.)
Re:The 2,500 year old history of why Python’s all([]) return... by ulucs on Wednesday March 18, 2020 @06:15AM (#22615109)
Replying to the sibling comment,

We invented mathematics because practical thinking leaves us without answers at some points. You are free to axiomatically choose "all([]) = false", but then you'll find out that many mathematical equivalences won't hold as they all assume only the ZF axioms.

Re:The 2,500 year old history of why Python’s all([]) return... by gridlockd on Wednesday March 18, 2020 @05:58AM (#22615046)
I don't disagree. Try to mute your "mathematical thinking" for a second and read it as an ordinary person, with a focus on practicality. It just doesn't look right.

If it did look right to me, I wouldn't be sitting here typing this all out. I would be silently agreeing with the mathematical point of view.

That's what tells me this is a likely source of confusion and error.

Re:The 2,500 year old history of why Python’s all([]) return... by umanwizard on Wednesday March 18, 2020 @08:51AM (#22616018)
I simply don’t have the intuition, at all, no matter how I think about it, that it should be false.

Is there any language that works the way you’re suggesting is more practical/intuitive?

Re:The 2,500 year old history of why Python’s all([]) return... by gridlockd on Wednesday March 18, 2020 @09:08AM (#22616182)
I'm not saying it should be false. It's the law of the excluded middle that forces us to make a true/false decision, in which case I can agree that, for mathematical reasons, it should be true.

For practical reasons, it might be better for it to be false. That would require some empirical research on whether this could actually prevent harmful behavior in practice.

From a design perspective, I would argue that "all" should not be defined for empty lists, because that forces people to consider whether the often-forgotten "empty list" edge case might cause an issue.

Re:The 2,500 year old history of why Python’s all([]) return... by nicoburns on Wednesday March 18, 2020 @09:45AM (#22616554)
It's the right choice for practical reasons as well as theoretical/mathematical ones. It means that you generally don't have to special case the empty list case, you can just run let it happily do nothing. If you want exceptional behavior then you should probably use some other value (e.g. None)
Re:The 2,500 year old history of why Python’s all([]) return... by gridlockd on Wednesday March 18, 2020 @11:23AM (#22617623)
> It means that you generally don't have to special case the empty list case, you can just run let it happily do nothing.

The consequence of "if (True)" is usually to do something, not nothing. The consequence of doing something by overlooking the "empty list" special case may be harmful.

Preventing potential harm is more important than not having the programmer jump through a couple of extra hoops to account for the special case. That makes the special case impossible to overlook.

This kind of reasoning is generally accepted when talking about implicitly nullable types vs explicitly nullable types (optional types), but somehow in this context people are more stubborn.

Re:The 2,500 year old history of why Python’s all([]) return... by eru on Thursday March 19, 2020 @10:02AM (#22627560)
> This kind of reasoning is generally accepted when talking about implicitly nullable types vs explicitly nullable types (optional types), but somehow in this context people are more stubborn.

Not sure that's the same? Haskell has no implicitly nullable types. Everything not explicitly marked as optional is non-nullable.

But still, if your algorithm at hand can treat nulls the same as non-nulls, it's a good idea to do so. (Eg in a sorting algorithm where you just feed the elements to the user supplied comparison function, and let that function handle comparing of optional elements.)

Re:The 2,500 year old history of why Python’s all([]) return... by int_19h on Thursday March 19, 2020 @01:56AM (#22624925)
Can you give a practical example of a case where that answer would actually be wrong for an empty set, though? Like, what are you trying to check, and what would be inside the "if"?