News for hackers. Stuff that matters.
Re:Please reconsider the Boolean evaluation of midnight by dalke on Thursday March 06, 2014 @08:12PM (#7357823)
If that were true, it would mean that dict should not support bool:

    >>> {} + {}
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    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 it this argument is pursuasive, should bool({}) raise an exception? Or should {1:"A"}+{1:"B"} not raise an exception and instead return ... what?

Or do I misunderstand something?

Re:Please reconsider the Boolean evaluation of midnight by baddox on Friday March 07, 2014 @04:59PM (#7363027)
Honestly, I would be fine with nothing supporting bool, and requiring `if` conditions to be bools. Arrays and dictionaries could have methods like `.empty?` that return a bool. Strings could have `.blank?`. Obviously, I'm stealing the methods from Ruby, although Ruby also has its own notions of truthiness of non-booleans.
Re:Please reconsider the Boolean evaluation of midnight by dalke on Friday March 07, 2014 @06:17PM (#7363399)
There are many languages which do something like that. Python is not one of them. Personally, I find it useful to write things like "if x:" and not worry about if x is None or x is s list of characters or x is a string of characters.
Re:Please reconsider the Boolean evaluation of midnight by baddox on Friday March 07, 2014 @06:55PM (#7363540)
I guess it comes down to the much more fundamental dichotomy between dynamic and static typing. I think most programmers would agree that having a function that accepts strings, lists, and dicts into the same argument (and thus sends them all through the exact same code path) is usually a bad idea. In Rails, I often find myself calling `to_i` or `to_s` just so that I can safely reason about that variable in the subsequent code.
Re:Please reconsider the Boolean evaluation of midnight by dalke on Friday March 07, 2014 @07:55PM (#7363744)
Which means you don't like generic programming or templates. That in turn tells me something about the types of programmers you know. For example, neither you nor they likely use the Boost libraries.

In any case, it's a bit of a distraction. "Most programmers" aren't all that good at programming, or judging what makes a good language. How important then is it that I weigh your projection of their ideas of right or wrong?

In Python it absolutely, positively, without a doubt is not a bad thing to have a function which accepts multiple data types. Some trivial examples are len() and iter(). Since you don't like the core language design in Python, I think it's safe to argue that your sensibilities are going to have many objections to any other part of the language.

Re:Please reconsider the Boolean evaluation of midnight by baddox on Friday March 07, 2014 @08:47PM (#7363900)
Do any of your examples (generic programming, templates, Boost libraries) actually utilize the concept of testing the truthiness of an object of unknown type? I think your criticism is invalid. I like generic programming in Java, though I admittedly have not used templates or Boost libraries for anything substantial.

> In Python it absolutely, positively, without a doubt is not a bad thing to have a function which accepts multiple data types. Some trivial examples are len() and iter().

I'm not arguing that this shouldn't be the case. My argument is specifically about bool(). If len() or iter() worked as strangely with basic data types as bool(), then I would probably extend my argument to cover them as well. For any data type I'm aware of that works with len() or iter(), the logic of what is returned is pretty clear and obvious, but that's not the case (in my opinion) with bool(). If len(-5) returned 5 (i.e. absolute value or "length from zero") then I would argue that it's a bad idea.

Re:Please reconsider the Boolean evaluation of midnight by dalke on Friday March 07, 2014 @09:54PM (#7364060)
C++ doesn't have unknown types, so we're working with different definitions. Here's an example:

    [xebulon:~/tmp] dalke% cat tmp.cc
    #include <iostream>
    
    template<typename T>
    int f(T s) {
     return s ? 2 : 0;
    }
    
    main() {
      std::cout << "int 0 " << f(0) << std::endl;
      std::cout << "int 8 " << f(8) << std::endl;
      std::cout << "float 0.0 " << f(0.0) << std::endl;
      std::cout << "float -1.0 " << f(-1.0) << std::endl;
    }
    [xebulon:~/tmp] dalke% g++ tmp.cc
    [xebulon:~/tmp] dalke% ./a.out 
    int 0 0
    int 8 2
    float 0.0 0
    float -1.0 2
The function f() doesn't know the type, but its instantiation for f(0) (integer) and f(0.0) float know the type.

C++ containers don't have a bool (or at least vector<> doesn't). For one, until C++11 there was no "explicit operator bool", and a simple "operator bool" was too permissive because of implicit type conversion. C++11 introduced a more contextual conversion to bool.

More and more components have explicit bool support. For example, http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/shared_p... ?

    Notes: This conversion operator allows shared_ptr objects to be
    used in boolean contexts, like if(p && p->valid()) {}.

    [The conversion to bool is not merely syntactic sugar. It allows shared_ptrs
    to be declared in conditions when using dynamic_pointer_cast
    or weak_ptr::lock.]
and http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_... .

I do not track C++ well enough to go into any more depth than this, especially as it concerns the history and future.

I like to say "if x: ..." and not "if len(x) == 0:" in order to check if a dictionary is empty.

Following Scheme, I can understand that something like "if empty(x)" might be more explicit. But I have a decent amount of code where I do something like:

   def process(a, rename=None):
     if rename:
       a = [rename.get(x, x) for x in a]
     ... do things with a ...
In this toy example, "rename=None" indicates that there is no renaming dictionary, and using {} as the renaming dictionary won't rename anything, so the "if rename" tests for both conditions correctly.

Without bool, I could write it as:

     if rename is not None:
since using the empty dictionary in this case is okay, but if I want the slight extra performance for the empty dictionary case I would have to write it:

     if rename is not None and not empty(rename):
I think that would grow tedious.

BTW, I don't use "def process(a, rename={}):" because that is one of Python anti-patterns: the default values are constant over the life of the function, so

    def something(a, b={}):
      b[a] = a
      return b
will accumulate to b rather than create a new dictionary each time. Thus, seeing a "={}" or "=[]" in a parameter list demands closer inspection because it often leads to errors.
Re:Please reconsider the Boolean evaluation of midnight by barakm on Friday March 07, 2014 @03:35PM (#7362573)
bool({}) should raise an exception, if we're being strict.

You could try to define the addition of keys like you suggest, but you need to force a value type which defines a + operation into the same type. Which is what Counter() does, below (appending strings).

Python is dynamically typed, so you can't maintain these expectations. Therefore, summation and bool({}) should be an error, or merely "true" (ie, yes, it exists, and is not None) -- as per the original thread and resolution.

Re:Please reconsider the Boolean evaluation of midnight by dalke on Friday March 07, 2014 @08:27PM (#7363837)
Good thing Python isn't a strict language then. That would be Scheme. ;)
Re:Please reconsider the Boolean evaluation of midnight by mctx on Thursday March 06, 2014 @08:54PM (#7357995)
{1: ["A", "B"]}?
Re:Please reconsider the Boolean evaluation of midnight by dalke on Thursday March 06, 2014 @09:22PM (#7358132)
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 by einhverfr on Friday March 07, 2014 @05:45AM (#7359529)
This could be interesting. One would have to define what hash math should mean. Personally, I would expect (based on my naive assumptions here) that:

    {1: "A"} + {} == {1: "A"}
but

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

    {1: ("A" + "B" + "C"), 2: "Z"}
Whether you can add "A", "B", and "C" would depend on string functions (could be "ABC" or raise an exception that these cannot be added for example).
Re:Please reconsider the Boolean evaluation of midnight by aiiane on Thursday March 06, 2014 @11:30PM (#7358515)
How does {1:"A"}+{} = {1:"A"} imply that {} is no longer a zero value? 1+0=1 makes total sense...
Re:Please reconsider the Boolean evaluation of midnight by dalke on Friday March 07, 2014 @12:36AM (#7358677)
It's not just that one expression, but when done in addition to the previous suggested answer. That is, what is the type of the values in the summed dictionaries?

If {1:"A"}+{} == {1:"A"} then the type of each value is unchanged from the input list.

If {1:"A"}+{1:"B"} == {1:["A","B"]} then the type of each value (or at least each shared value) is a 2-element list.

If both are true, then no one can write sane code which knows how deal with the sum of two dictionaries.

That is, the insane code has to have checks for if one of the dictionaries is empty, and if so, do one thing, otherwise do another thing. In which case, why is this definition useful?

Therefore, either {} is the zero value, or dictionary summation produces lists for the resulting values, but not both.

Re:Please reconsider the Boolean evaluation of midnight by anamexis on Friday March 07, 2014 @01:55AM (#7358894)
In the comment above, the hypothetical result is {1:["A"]}, not {1:"A"}. (The value is in a list.)
Re:Please reconsider the Boolean evaluation of midnight by cuttle on Thursday March 06, 2014 @09:06PM (#7358064)
Very little code that expects "A" or "B" would be equipped to handle ["A","B"].
Re:Please reconsider the Boolean evaluation of midnight by sesqu on Friday March 07, 2014 @06:43AM (#7359695)
{1:"AB"}

That's how it usually works with functions.

Re:Please reconsider the Boolean evaluation of midnight by dalke on Friday March 07, 2014 @08:02AM (#7359926)
If you want that, try the Counter class:

    >>> from collections import Counter
    >>> Counter({1: "a"}) + Counter({1: "b"})
    Counter({1: 'ab'})
But Python dictionaries won't support '+' because of the ambiguity. This goes back to 1994: http://ftp.ntua.gr/mirror/python/search/hypermail/python-199...

    Lance Ellinghouse:
    why can't I add dictionaries like I can other objects?
    ...
    if a and b both had the same keys, then I think b should override
    the keys in a.
and Kenneth Manheimer posted a dict-like implementation in http://ftp.ntua.gr/mirror/python/search/hypermail/python-199... which also has the replacement behavior.

Guido responded in http://ftp.ntua.gr/mirror/python/search/hypermail/python-199... saying that he could think of no reason to prefer replacement over keep behavior.

In my own early Python code, I sometimes used + when I wanted an update semantic, so I can confirm that your expectations aren't universal.

And that's what this comes down to - is bool(time(0, 0))==False the generally expected behavior (which is a higher threshold than reasonable behavior)? What are the likely errors? Do the advantages outweigh the disadvantages?

That's why an argument about the zero element is only somewhat relevant - most programmers aren't mathematicians, and practicality beats purity.

Re:Please reconsider the Boolean evaluation of midnight by dalke on Friday March 07, 2014 @11:19AM (#7361040)
This is neat:

    >>> a = Counter()
    >>> a[1] = a
    >>> b = Counter()
    >>> b[1] = b
    >>> a + b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "...../collections.py", line 536, in __add__
        newcount = self[elem] + other[elem]
      File "...../collections.py", line 534, in __add__
        result = Counter()
    RuntimeError: maximum recursion depth exceeded
If dict() implemented add as you propose then it would be subject to the same errors. This observation doesn't make your proposal wrong. I mean it to point out the subtle complexities which might come up if a large number of people don't know what it's supposed (and even documented) to do.
Re:Please reconsider the Boolean evaluation of midnight by sesqu on Saturday March 08, 2014 @03:48AM (#7364683)
That's an interesting point, but I'm inclined to say that very few languages could come up with {1:this} - Prolog could, I think - and that running into those errors in Python would be user error.

That said, this is a good example of the expressivity of this, which mathematical definitions often implicitly disallow.

Re:Please reconsider the Boolean evaluation of midnight by dalke on Saturday March 08, 2014 @11:09AM (#7365520)
An analysis shouldn't stop at "user error". Instead, ask if the programming language design plays a role.

For example, NUL terminated strings lead to a lot of user errors, some of which lead to security holes. Other string representations don't have that flaw, though come with a different cost concern. Is a buffer overflow "user error"? Some would say it is. But the language design makes those errors more dangerous.

Or in Python, there's no technical reason to have a ":" at the end of the line before an indented block. Instead, it's there because user studies show that people learning ABC (which influenced a lot of early Python) made fewer mistakes if the ":" was present than if it wasn't.

Are indentation mistakes user error? Certainly users play a role. But again, the design does as well.

So saying that something is a "user error" with no further analysis absolves the designer of any responsibility, and I disagree with that idea.

There are at least 4 different ways to handle dict+dict in Python. At least three have come up in this thread as the proposed correct solution. Even if there is a mathematically clean solution, if only 10% of the people expect it to work that way, then why should Python introduce something which is so error prone? No support for dict+dict is 100% error prone, of course, but trivially identified in testing. While Counter+Counter-like behavior has subtle consequences that will trip people up.

(As another example of the subtleties, consider an inverted index mapping word to a list of document ids:

    collection_a = {"a": [0, 3, 4], "the": [0, 2, 3]}
    collection_b = {"a": [5], "an": [6, 7]}
    collection_c = {"the": [10], "not": [10]}
    merged_collection = a+b
    merged_collection += collection_c
This is wrong because the += changes merged_collection["the"], which is the same list as collection_a["the"]. So even though it looks like good code, and it is good code for any value where x+=y is the same as x=x+y, it may cause problems which are hard to spot.)

"In the face of ambiguity, resist the temptation to guess."