Restore `numpy.random.seed` accidentally overwritten in interactive session

Viewed 53

I was in the middle of a reasonably complicated interactive python session, and I had a lot of state that I didn't want to have to regenerate. (Normally I wouldn't be doing that -- it's pretty stupid -- but sometimes...)

Instead of numpy.random.seed(42) I typed by accident numpy.random.seed = 42 thus making the numpy.random.seed() function unreachable.

I hoped that import numpy might fix it, but it didn't.

I'm curious to know if there is a general method for recovering from this kind of daft mistake? Or even a specific one?

1 Answers

First you need to load the importlib library, and then tell it to reload numpy.random.

import importlib
importlib.reload(numpy.random)

Note that it's not enough to reload numpy itself, since that will just do an ordinary import on random, and finding it already there won't make any difference.

But you're right -- you should try to avoid getting into a situation like this with complicated state.

Related