Re:WTFPython – Understanding Python through surprising snippets
by aidos on Monday December 23, 2019 @04:11AM
(#21862376)
After a long time using both languages, I’d argue that non-sane equality checks in js is the biggest source of bugs.
[ Parent
| on HN ]
Re:WTFPython – Understanding Python through surprising snippets
by tyri_kai_psomi on Monday December 23, 2019 @04:34AM
(#21862483)
Unfortunately, this is not true. Although the equality operators are a favorite bikeshedding topic, the largest source of errors in JS code bases are type errors. Using a "non-sane" equality check, such as those with type coercion would actually mask or alleviate these source of bugs you mention.
Re:WTFPython – Understanding Python through surprising snippets
by aidos on Monday December 23, 2019 @04:41AM
(#21862521)
I was thinking more of the case where the types match but objects and arrays with the same contents are considered different. I’ve watched every member of my team get stung by it again and again - and then have to create workarounds to get past it.
Re:WTFPython – Understanding Python through surprising snippets
by CallocRoast on Monday December 23, 2019 @04:48AM
(#21862552)
I don’t agree and this behavior shouldn’t be surprising. The alternative would be to walk the container and compare the value of each element, which could be horrible.
Re:WTFPython – Understanding Python through surprising snippets
by BiteCode_dev on Monday December 23, 2019 @05:19AM
(#21862675)
Quite the contrary, it's very beautiful and useful:
>>> (1, 2) == (1, 2)
True
>>> (2, 1) == (1, 2)
False
If you need identity for perf: >>> (1, 2) is (1, 2)
False
If you just need the type check: >>> isinstance((1, 2), type((1, 2)))
True
It's very explicit, practical, and you can set the scale of practicality vs performances where you want. Plus: no implicit weird type conversion, only one equality comparison operator, and no hidden rules.I think it's sane.
Re:WTFPython – Understanding Python through surprising snippets
by aidos on Monday December 23, 2019 @05:23AM
(#21862698)
I’m not sure if you’re for or against here. Walking the container is exactly what you have to do, and it is horrible. More importantly, if it’s not your library, you don’t get any choice on how the equality check is implemented.