2
2

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 3 years have passed since last update.

jupyter上でのmoduleの使い方

2
Posted at

jupyter上でのModuleの使い方

一言で言うと

moduleの中でstructを定義しておけば、structに変更を加えてもjupyterのカーネルを再起動する手間が省けるという話です。

本題

juliaでstructを定義し、後から変更を加えたい場合、カーネルを再起動しなければならない。例えば、

struct Hoge
    x::Int
end
h = Hoge(3)
h.x # 3

これに後からyというものをstructのフィールドに加えようとし、セルを実行してみる。すると、カーネルを再起動しなければいけなく面倒である。

struct Hoge
    x::Int
    y::Int
end

# エラーが出る

ここで、以下のようにmodule化しておくと、sturctに変更を加えてもエラーが出ることはない。

module My

struct Hoge
    x::Int
end

end

using .My
Hoge = My.Hoge
h = Hoge(3)
h.x

変更を加えてみるとエラー自体は出ないことがわかる。

module My

struct Hoge
    x::Int
    y::Int
end

end

using .My
Hoge = My.Hoge
h = Hoge(3,4)
h.x
h.y

structを作り直すごとにMyモジュールを使って、Hogeを書き直している。今いるmoduleは、メインっていうモジュールにいて、MyモジュールにいるHogeをメインに持ってきている。この場合、エラーが出ることはない。

メインモジュールにいるstructを書き直すと、カーネル再起動しないといけない。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?