Skip to content

Instantly share code, notes, and snippets.

@dstu
Created March 21, 2013 22:12
Show Gist options
  • Save dstu/5217272 to your computer and use it in GitHub Desktop.
Save dstu/5217272 to your computer and use it in GitHub Desktop.
Shuffling a list with a numpy.random.RandomState forgets the type of list elements if they're tuples.
import pkg_resources
from collections import namedtuple
from numpy.random import RandomState
print pkg_resources.get_distribution("numpy").version
class Foo(namedtuple("Foo", "x")):
pass
items = [Foo(1), Foo(2), Foo(3)]
print type(items[0])
rng = RandomState()
rng.shuffle(items)
print type(items[0])
@dstu
Copy link
Author

dstu commented Mar 21, 2013

Running numpy 1.6 in Python 2.6.4 on x86-64 Linux, the first "print type(items[0])" prints 'main.Foo', while the second prints 'tuple'. After asking in #python on Freenode, it looks like the type of the list elements isn't forgotten in numpy 1.8.0.dev-75b8119 in Python 2.7.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment