Are dicts supposed to raise comparison errors

Chris Angelico rosuav at gmail.com
Wed Aug 1 09:38:25 EDT 2018


On Wed, Aug 1, 2018 at 11:25 PM, Robin Becker <robin at reportlab.com> wrote:
> messing with bytes I discover that this doesn't warn with python -b
>
> ########################
> if __name__=='__main__':
>     class nbytes(bytes):
>         def __eq__(self,other):
>             return bytes.__eq__(self,other) if isinstance(other,bytes) else
> False
>         def __hash__(self):
>             return bytes.__hash__(self)
>     d={'a':1}
>     d[nbytes(b'a')] = d['a']
>     print(d)
>     d={nbytes(b'a'):1}
>     d['a'] = d[b'a']
>     print(d)
> ########################
>
>
>> C:\code\hg-repos\reportlab\tmp>\python37\python -b tb1.py
>> {'a': 1, b'a': 1}
>> {b'a': 1, 'a': 1}
>
>
> I expected one of the assignments to warn.

It's a warning designed to help people port code from Py2 to Py3. It's
not meant to catch every possible comparison. Unless you are actually
porting Py2 code and are worried that you'll be accidentally comparing
bytes and text, just *don't use the -b switch* and there will be no
problems.

I don't understand what the issue is here.

ChrisA



More information about the Python-list mailing list