5 ms·
If that were true, it would mean that dict should not support bool: >>> {} + {} Traceback (most recent call last): File "<stdin>", line 1, in <mo
by dalke 13y ago
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?
- mctx 13y ago{1: ["A", "B"]}?
- cuttle 13y agoVery little code that expects "A" or "B" would be equipped to handle ["A","B"].
- dalke 13y agoOkay, 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."
- aiiane 13y agoHow does {1:"A"}+{} = {1:"A"} imply that {} is no longer a zero value? 1+0=1 makes total sense...
- dalke 13y agoIt'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.
- anamexis 13y agoIn the comment above, the hypothetical result is {1:["A"]}, not {1:"A"}. (The value is in a list.)
- einhverfr 13y agoThis 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).
- sesqu 13y ago{1:"AB"} That's how it usually works with functions.
- dalke 13y agoIf 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-1994q3/0176.html 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-1994q3/0115.html 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-1994q3/0179.html 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.
- dalke 13y agoThis 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.
- barakm 13y agobool({}) 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.
- dalke 13y agoGood thing Python isn't a strict language then. That would be Scheme. ;)
- baddox 13y agoHonestly, 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.
- dalke 13y agoThere 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.
- baddox 13y agoI 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.
- dalke 13y agoWhich 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.
- 13y ago