[ python-Bugs-1022880 ] random.shuffle should restrict the type of its argument

SourceForge.net noreply at sourceforge.net
Mon Sep 6 08:18:12 CEST 2004


Bugs item #1022880, was opened at 2004-09-06 04:45
Message generated for change (Comment added) made by fmitha
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1022880&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Faheem Mitha (fmitha)
Assigned to: Nobody/Anonymous (nobody)
Summary: random.shuffle should restrict the type of its argument 

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: Faheem Mitha (fmitha)
Date: 2004-09-06 06:18

Message:
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.



----------------------------------------------------------------------

Comment By: Faheem Mitha (fmitha)
Date: 2004-09-06 06:14

Message:
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

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2004-09-06 05: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.  

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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1022880&group_id=5470


More information about the Python-bugs-list mailing list