This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: random.shuffle should restrict the type of its argument
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: fmitha, rhettinger
Priority: normal Keywords:

Created on 2004-09-06 04:45 by fmitha, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg22363 - (view) Author: Faheem Mitha (fmitha) Date: 2004-09-06 04:45
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.
msg22364 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-09-06 05:16
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.
msg22365 - (view) Author: Faheem Mitha (fmitha) Date: 2004-09-06 06:14
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
msg22366 - (view) Author: Faheem Mitha (fmitha) Date: 2004-09-06 06:18
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.

msg22367 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-09-06 07:10
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.
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40872
2004-09-06 04:45:56fmithacreate