简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM