1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Jupyter Notebookで名前空間を汚さない工夫

Posted at

Jupyter notebookは名前空間が汚れて大変なことになるので、極力使っていないのですが、解析が実験ノートの一部になるときはなんだかんだで便利なので使っています。名前空間が汚れるのを防ぐために次のようなことをしています。

1. runを定義

デコレートした関数を実行するだけの関数です。

def run(f):
    return f()

2. main関数みたいなものを各セルで定義

@run
def calculate_mean():
    data = np.random.random(100)
    print(np.mean(data))

これでセルを実行すると関数が勝手に実行されます。当然変数は関数内でしか有効ではないので、名前空間は汚れません。関数名自体と変数名が衝突しないように、関数名は内容が分かるようにという意味でもすごく長いのにしましょう。

使ってみてください。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?