Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2025/06/19 08:39:27.157 (台灣標準時)

自分独自の Python のモジュールを作って使う方法

自分独自の Python のモジュールを作り、そして、それを使う方法を記録して おくでござる。 "The Python Tutorial" の第六節 "Modules" を読めばよいようでござる。

単独のファイルに関数を書き込んで使う

まず、以下のような内容のファイルを用意し、 "mymodules.py" と いう名前にするでござる。


#
# Time-stamp: <2025/06/11 16:05:26 (UT+08:00) daisuke>
#

# a function to add two numbers
def add_two_numbers (a, b):
    # calculation
    c = a + b
    # returning result of calculation
    return c

この "mymodules.py" の中の add_two_numbers () という関数を使 うには、以下のようにすればよいようでござる。


#!/usr/pkg/bin/python3.13

#
# Time-stamp: <2025/06/11 16:07:19 (UT+08:00) daisuke>
#

# importing "mymodules.py"
import mymodules

# x = 1.2, y = 3.4
x = 1.2
y = 3.4

# adding x and y
z = mymodules.add_two_numbers (x, y)

# printing result of calculation
print (f'{x} + {y} = {z}')

実行結果は以下の通りでござる。


% python3.13 test_mymodules_00.py
1.2 + 3.4 = 4.6

複数のファイルに関数を書き込んで使う

次に、 "mynewmodules" というディレクトリを作り、そのディレク トリの中に "myarithmetic.py" と "mytrigonometric.py"という二つのファイルを作り、それぞれのファ イルに関数を書き込んで、それらの関数を別の Python スクリプトから呼び出 して使う、ということをやってみるでござる。


mynewmodules
├── myarithmetic.py
└── mytrigonometric.py

"myarithmetic.py" の内容は以下のとおりでござる。

#
# Time-stamp: <2025/06/11 16:05:44 (UT+08:00) daisuke>
#

# a function to add two numbers
def add_two_numbers (a, b):
    # calculation
    c = a + b
    # returning result of calculation
    return c

# a function to multiply two numbers
def multiply_two_numbers (a, b):
    # calculation
    c = a * b
    # returning result of calculation
    return c

"mytrigonometric.py" の内容は以下のとおりでござる。

#
# Time-stamp: <2025/06/11 16:05:53 (UT+08:00) daisuke>
#

# importing math module
import math

# value of pi
pi = math.pi

# a function to calculate sin (x) for given x
def mysin (angle, unit):
    # angle in radian
    if (unit == 'radian'):
        angle_rad = angle
    elif (unit == 'degree'):
        angle_rad = angle / 180.0 * pi
    else:
        print (f'ERROR:')
        print (f'ERROR: unit must be either "radian" or "degree"')
        print (f'ERROR:')
    # calculation of sin (angle)
    sin_angle = angle_rad - angle_rad**3 / (2*3) + angle_rad**5 / (2*3*4*5) \
        - angle_rad**7 / (2*3*4*5*6*7) + angle_rad**9 / (2*3*4*5*6*7*8*9) \
        - angle_rad**11 / (2*3*4*5*6*7*8*9*10*11) \
        + angle_rad**13 / (2*3*4*5*6*7*8*9*10*11*12*13) \
        - angle_rad**15 / (2*3*4*5*6*7*8*9*10*11*12*13*14*15) \
        + angle_rad**17 / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17)
    # returning sin (angle)
    return sin_angle

"myarithmetic.py" と "mytrigonometric.py" の中の関 数を、別の Python スクリプトから使うには以下のようにすればよいようでご ざる。


#!/usr/pkg/bin/python3.13

#
# Time-stamp: <2025/06/11 16:06:18 (UT+08:00) daisuke>
#

# importing "mynewmodules"
import mynewmodules.myarithmetic
import mynewmodules.mytrigonometric

# adding two numbers
a = 1.23
b = 4.56
c = mynewmodules.myarithmetic.add_two_numbers (a, b)
print (f'{a} + {b} = {c}')

# multiplying two numbers
x = 7.8
y = 9.0
z = mynewmodules.myarithmetic.multiply_two_numbers (x, y)
print (f'{x} x {y} = {z}')

# calculating sin (30 deg)
angle_deg = 30.0
sin_30deg = mynewmodules.mytrigonometric.mysin (angle_deg, unit='degree')
print (f'sin ({angle_deg} deg) = {sin_30deg}')

実行結果は以下の通りでござる。


% python3.13 test_mynewmodules_00.py
1.23 + 4.56 = 5.789999999999999
7.8 x 9.0 = 70.2
sin (30.0 deg) = 0.49999999999999994



Frequently accessed files

  1. Computer___Python/20220518_0.html
  2. Computer___Network/20230726_00.html
  3. Misc___Taiwan/20240207_00.html
  4. Computer___Network/20230516_00.html
  5. Computer___FreeBSD/20220621_0.html
  6. Computer___Network/20240130_00.html
  7. Computer___Network/20230508_00.html
  8. Computer___Python/20220715_0.html
  9. Computer___TeX/20231107_00.html
  10. Computer___NetBSD/20230119_00.html
  11. Food___Taiwan/20220429_0.html
  12. Computer___Network/20240416_00.html
  13. Computer___Network/20220413_1.html
  14. Computer___Python/20220410_0.html
  15. Misc___Japan/20240610_00.html
  16. Computer___Python/20221013_0.html
  17. Computer___TeX/20230726_01.html
  18. Computer___Python/20240101_00.html
  19. Computer___NetBSD/20220817_3.html
  20. Computer___Debian/20210223_1.html
  21. Computer___NetBSD/20220818_1.html
  22. Computer___NetBSD/20240101_02.html
  23. Computer___Python/20210124_0.html
  24. Computer___NetBSD/20220428_0.html
  25. Misc___Taiwan/20240819_00.html
  26. Science___Math/20220420_0.html
  27. Computer___TeX/20240414_00.html
  28. Computer___TeX/20230503_00.html
  29. Computer___NetBSD/20240101_03.html
  30. Science___Astronomy/20220503_0.html


HTML file generated by Kinoshita Daisuke.