' P '

whatever I will forget

Python モジュールのimport

C言語でいうところの#includeですね。
pythonはheaderとかにせず、importして使う感じっす。
さっきの関数のコードを使い回し。

calc_main.py

import calc_tax as c

total = c.calc_tax(percent=10, reduction=1, price=1000)
print (total)
total = c.calc_tax(1000, 10)
print (total)
total = c.calc_tax(1000, 15, 3)
print (total)

cacl_tax.py

def calc_tax (price, percent=8, reduction=0):
    #calcualte price with tax
    return price * (1+(percent - reduction)/100)

もちろん、デフォルトとしてpythonから提供されている有名なモジュールとしては
- sys (システムパラメータ)
- re (正規表現
- math (数学関数)
- random (乱数生成)
とかがありますね。これらをimportするのがよくみる例です。

また、モジュールを全部importしたくないよ、という場合は下記のようにします。

from ceil import math
こうすると、mathモジュールからceil関数だけ使えるようになります。