[issue33572] False/True as dictionary keys treated as integers

2018-05-18 Thread Terry J. Reedy

Change by Terry J. Reedy :


--
assignee:  -> docs@python
components: +Documentation -Interpreter Core
nosy: +docs@python

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33572] False/True as dictionary keys treated as integers

2018-05-18 Thread Tim Peters

Tim Peters  added the comment:

I expect these docs date back to when ints, longs, and floats were the only 
hashable language-supplied types for which mixed-type comparison could ever 
return True.

They could stand some updates ;-)  `fractions.Fraction` and `decimal.Decimal` 
are more language-supplied numeric types that participate now, and the Boolean 
singletons are instances of (a subclass of) `int`.  I think it would be good to 
point that out, especially the latter (in many other languages Booleans can't 
be compared to ints).

So +1 both to Mark's more-general explanation, and to explicitly naming more 
specific cases for illustration.

--
nosy: +tim.peters

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33572] False/True as dictionary keys treated as integers

2018-05-18 Thread Terry J. Reedy

Terry J. Reedy  added the comment:

Mark, are you suggesting a doc addition (and a change of Components) or should 
we close this as 'not a bug'?

--
nosy: +terry.reedy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33572] False/True as dictionary keys treated as integers

2018-05-18 Thread Mark Dickinson

Mark Dickinson  added the comment:

It's documented here: 
https://docs.python.org/3/library/stdtypes.html#mapping-types-dict

> Numeric types used for keys obey the normal rules for numeric
> comparison: if two numbers compare equal (such as 1 and 1.0) then 
> they can be used interchangeably to index the same dictionary entry.

Since False == 0, False and 0 are interchangeable as dictionary keys. Similarly 
for True and 1. Note that the dictionary you created only actually has two 
entries:

>>> dta = {False: 'false', True: 'true', 0: 'zero', 1: 'one'}
>>> dta
{False: 'zero', True: 'one'}

Though calling out numeric types in particular in the docs does feel a little 
odd to me: the rule is that *any* two hashable objects that compare equal 
should be interchangeable for the purposes of dictionary lookup (or set 
membership, come to that). There's nothing particularly special about numbers 
in this context.

--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33572] False/True as dictionary keys treated as integers

2018-05-18 Thread Janusz Harkot

Janusz Harkot  added the comment:

Python 3.6.5 (default, Mar 29 2018, 03:28:50)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dta = {False: 'false', True: 'true', 0: 'zero', 1: 'one'}
>>> print(dta[False])
zero
>>> dta
{False: 'zero', True: 'one'}
>>>

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33572] False/True as dictionary keys treated as integers

2018-05-18 Thread Janusz Harkot

New submission from Janusz Harkot :

using boolean (True/False) as dictionary keys, coerce them to integers - is 
this behavior documented somewhere?

I know that asking to fix this is not easy fix, but shouldn't this be 
highlighted everywhere with red flags and warnings, so people will know that 
this is expected?


Python 3.6.5 (default, Mar 29 2018, 03:28:50)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dta = {False: 'false', True: 'true', 0: 'zero', 1: 'one'}
>>> print(dta[False])
zero
>>>

--
components: Interpreter Core
messages: 317032
nosy: Janusz Harkot
priority: normal
severity: normal
status: open
title: False/True as dictionary keys treated as integers
type: behavior
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com