[issue37708] Harmonize random.choice(s) behavior with random.sample on iterators

2020-02-28 Thread Roundup Robot


Change by Roundup Robot :


--
nosy: +python-dev
nosy_count: 3.0 -> 4.0
pull_requests: +18055
pull_request: https://github.com/python/cpython/pull/18694

___
Python tracker 

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



[issue37708] Harmonize random.choice(s) behavior with random.sample on iterators

2019-07-31 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

The sample() function accepts d.keys() because a keys view is registered as 
being an instance of collections.abc.Set.  We have to do that because sample() 
is specifically documented as accepting sets.  If we had it do over again, that 
functionality would likely not be supported (it implicitly the population into 
a tuple).

For a fine-grained function like choice() that implicit population conversion 
would make no sense at all.  For choices(), we can choose not to make that API 
mistake again.

Thanks for the suggestion, but I am going to decline.  It isn't something that 
we need in practice and it would add complication for near zero gain.  (Also, 
there is no rule that all of these functions have to accept the same kinds of 
inputs -- different algorithms and different use cases allow for some 
flexibility).

--
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

___
Python tracker 

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



[issue37708] Harmonize random.choice(s) behavior with random.sample on iterators

2019-07-31 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee:  -> rhettinger

___
Python tracker 

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



[issue37708] Harmonize random.choice(s) behavior with random.sample on iterators

2019-07-29 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Seems similar to https://bugs.python.org/issue37682

--
nosy: +rhettinger, xtreak

___
Python tracker 

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



[issue37708] Harmonize random.choice(s) behavior with random.sample on iterators

2019-07-29 Thread Lex Flagel


New submission from Lex Flagel :

It would be nice to make random.sample and random.choice both have the same 
behavior with iterators. Currently random.sample accepts them happily, and 
whereas random.choice does not.  E.g.

> import random
> d = {'a':1, 'b':2}
> random.sample(d.keys(),1)
Out: ['a']

> random.choice(d.keys())
Out: TypeError: 'dict_keys' object is not subscriptable

random.choice could be redefined as follows to harmonize behavior, but I think 
the solution for random.choices maybe be more involved:

def choice(x): 
random.sample(x,1)[0]

--
components: Library (Lib)
messages: 348680
nosy: Lex Flagel
priority: normal
severity: normal
status: open
title: Harmonize random.choice(s) behavior with random.sample on iterators
type: enhancement
versions: Python 3.8

___
Python tracker 

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