Help us understand the problem. What is going on with this article?

Pythonで簡単に自作関数を使いまわす

More than 5 years have passed since last update.

まず自作関数を作ります.

myplot.py
import matplotlib.pyplot as plt

def myplot(x):
    plt.plot(x, 'o-')
    plt.show()

if __name__ == '__main__':
    x = [i**2 for i in range(20)]
    myplot(x)

ここでファイル名と関数名を同じにするのが好きです.

figure_1.png

これを自作の関数集フォルダに入れておきます.

mymodule/
  __init__.py
  myplot.py

中にこのファイルをおいておけば,

__init__.py
from myplot import myplot

mymodule からストレスなく myplot をインポートできます.

test_myplot.py
from mymodule import myplot

x = [(-0.5)**i for i in range(20)]
myplot(x)

figure_1.png

ただしこの場合,実行した test_myplot.py は mymodule と同じフォルダにある必要があります.

mymodule
test_myplot.py

そこで,自作関数集を特定のフォルダに集めておきます.

/home/okadate/pyfiles/
  mymodule
  mymodule2

自由な位置にある test_myplot.py をこうしておけば,

test_myplot.py
import sys; sys.path.append('/home/okadate/pyfiles')
from mymodule import myplot

x = [i/(5.0+i) for i in range(30)]
myplot(x)

figure_1.png

使えます!

他にもパッケージ化してしまうなどの方法があるみたいです.
簡単ならやってみたいけど..

okadate
数値モデルを使った沿岸域の生態系に関する研究をしています.計算結果の可視化・解析にPythonを使うので,メモ・勉強用の内容を投稿します.
http://okadate.github.io
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした