[New-bugs-announce] [issue33098] add implicit conversion for random.choice() on a dict
Aristide Grange
report at bugs.python.org
Sun Mar 18 18:00:28 EDT 2018
New submission from Aristide Grange <aristide.grange at gmail.com>:
In Python 3, the expression:
```python
random.choice(d)
```
where `d` is a `dict`, raises this error:
```
~/anaconda3/lib/python3.6/random.py in choice(self, seq)
256 except ValueError:
257 raise IndexError('Cannot choose from an empty sequence') from None
--> 258 return seq[i]
259
260 def shuffle(self, x, random=None):
KeyError: 2
```
Converting `d` into a list restores the Python 2's behavior:
```python
random.choice(list(d))
```
I am aware that the keys of a dict have now their own type. But IMHO the error message is rather uninformative, and above all, couldn't this conversion be made implicitely under the hood?
----------
messages: 314062
nosy: Aristide Grange
priority: normal
severity: normal
status: open
title: add implicit conversion for random.choice() on a dict
type: enhancement
versions: Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33098>
_______________________________________
More information about the New-bugs-announce
mailing list