Are dicts supposed to raise comparison errors

Peter Otten __peter__ at web.de
Wed Aug 1 06:00:54 EDT 2018


Robin Becker wrote:

> On 01/08/2018 09:52, Chris Angelico wrote:
>> On Wed, Aug 1, 2018 at 6:36 PM, Robin Becker <robin at reportlab.com> wrote:
>>> On 31/07/2018 16:52, Chris Angelico wrote:
>>..........
>>>
>>> it says explicitly that numeric keys will use numeric comparison, but no
> .........
>>>
>> 
>> Technically, the comparison used is:
>> 
>> a is b or a == b
>> 
>> in other words, identity will match, but mainly, equality is used. The
>> identity check improves performance in many common cases, and also
>> avoids pathological cases involving float("nan"), but in general
>> discussion, it's assumed that value rather than identity is the
>> comparison used.
>> 
>> ChrisA
>> 
> 
> If the testing were done in that order then no comparison warning would
> occur as inspection shows 'a' is not b'a'

Nope, that would be the effect of "and", not "or".

>>> "a" is b"b" and "fallback"
False
>>> "a" is b"b" or "fallback"
'fallback'

You seem to be caught in your wrong mental model. I recommend that you let 
the matter rest for a day or so, and then look at it with a fresh eye.
 
>   > C:\code\hg-repos\reportlab\tmp>\python37\python.exe -b -Wall
>> Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64
>> bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license"
>> for more information.
>>>>> 'a' is b'a'
>> False
>>>>>
> 
> so presumably the testing goes something like
> 
> a is b or (a==b if comparable(a,b) else False)
> 
> even so I still think errors/warnings created internally by implementers
> should not be exposed. It's not a big deal. I'm a bit surprised that we
> don't have a mapping which uses only identity as that would be faster.





More information about the Python-list mailing list