Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random.shuffle should restrict the type of its argument #40872

Closed
fmitha mannequin opened this issue Sep 6, 2004 · 5 comments
Closed

random.shuffle should restrict the type of its argument #40872

fmitha mannequin opened this issue Sep 6, 2004 · 5 comments
Labels
stdlib Python modules in the Lib dir

Comments

@fmitha
Copy link
Mannequin

fmitha mannequin commented Sep 6, 2004

BPO 1022880
Nosy @rhettinger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2004-09-06.07:10:00.000>
created_at = <Date 2004-09-06.04:45:56.000>
labels = ['library']
title = 'random.shuffle should restrict the type of its argument '
updated_at = <Date 2004-09-06.07:10:00.000>
user = 'https://bugs.python.org/fmitha'

bugs.python.org fields:

activity = <Date 2004-09-06.07:10:00.000>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2004-09-06.04:45:56.000>
creator = 'fmitha'
dependencies = []
files = []
hgrepos = []
issue_num = 1022880
keywords = []
message_count = 5.0
messages = ['22363', '22364', '22365', '22366', '22367']
nosy_count = 2.0
nosy_names = ['rhettinger', 'fmitha']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1022880'
versions = ['Python 2.3']

@fmitha
Copy link
Mannequin Author

fmitha mannequin commented Sep 6, 2004

Consider the following


In [1]: foo = numstr.array(['a', 'c'], shape=(2,1))
In [2]: import random
In [3]: import numarray.strings as numstr
In [4]: foo
Out[4]:
CharArray([['a'], ['c']])
In [5]: random.shuffle(foo)
In [6]: foo
Out[64]:
CharArray([['a'], ['a']])


The documentation says that shuffle takes a list as
argument. In this case it accepts a Numarray character
array without complaint and returns gibberish. I think
there should be some restrictions on what arguments are
accepted. Thanks.

                Faheem.

@fmitha fmitha mannequin closed this as completed Sep 6, 2004
@fmitha fmitha mannequin added the stdlib Python modules in the Lib dir label Sep 6, 2004
@rhettinger
Copy link
Contributor

Logged In: YES
user_id=80475

While in your case it worked out badly, python's duck typing
is a core design principle. It is a feature that mutable
sequences (listlike objects) can be passed as arguments and
shuffle does the riight thing.

It is unfornate that the 2D arrays look like lists (having
__len__, __getitem__, and __setitem__) but behave in ways
that are unlike other sequences. For your use case, first
flatten the array and then shuffle it:
x = foo.tolist()
shuffle(x)

So, this is not a bug but I'm leaving it open for a bit in
case others want to comment.

@fmitha
Copy link
Mannequin Author

fmitha mannequin commented Sep 6, 2004

Logged In: YES
user_id=1063525

The max recursion limit problem in the re module is well-known.
Until this limitation in the implementation is removed, to work
around it check

http://www.python.org/dev/doc/devel/lib/module-re.html
http://python/org/sf/493252

@fmitha
Copy link
Mannequin Author

fmitha mannequin commented Sep 6, 2004

Logged In: YES
user_id=1063525

> Comment By: Raymond Hettinger (rhettinger)
Date: 2004-09-06 00:16

Message:
Logged In: YES
user_id=80475

While in your case it worked out badly, python's duck typing
is a core design principle. It is a feature that mutable
sequences (listlike objects) can be passed as arguments and
shuffle does the riight thing.

Yes, but in this case, it does not do the right thing.

It is unfornate that the 2D arrays look like lists (having
__len__, __getitem__, and __setitem__) but behave in ways
that are unlike other sequences. For your use case, first
flatten the array and then shuffle it:
x = foo.tolist()
shuffle(x)

Yes, that is what I did.

So, this is not a bug but I'm leaving it open for a bit in
case others want to comment.

                                               Faheem.

@rhettinger
Copy link
Contributor

Logged In: YES
user_id=80475

I don't see a way to detect your issue without breaking
shufflle's current capability of working with a variety of
mutable sequences. Changing that would likely break code
that is currently working fine.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

1 participant