teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2019/01/09 08:55

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -1,3 +1,40 @@
1
1
  `myclass.py`は[モジュール検索パス](https://docs.python.jp/3/tutorial/modules.html#the-module-search-path)に記載されているルールおよび場所から検索されますが、見つからないと`import`できません。
2
2
  一番単純な解決策は3つのファイルをすべて同じ場所に配置することです。
3
- この場合、重複している`myclass.py`ファイル名はどちらか変更する必要があります。
3
+ この場合、重複している`myclass.py`ファイル名はどちらか変更する必要があります。
4
+
5
+ #### 追記
6
+ 書いてみました。なおファイル名は分かりやすいように書き換えました。
7
+ ```Python
8
+ # persion.py
9
+ class Person:
10
+ def __init__(self, name, height, weight):
11
+ self.name = name
12
+ self.height = height
13
+ self.weight = weight
14
+
15
+ def bmi(self):
16
+ print(self.weight / self.height / self.height)
17
+ ```
18
+ ```Python
19
+ # buisiness_persion.py
20
+ from person import Person
21
+ class BusinessPerson(Person):
22
+ def __init__(self, name, height, weight, title):
23
+ super().__init__(name, height, weight)
24
+ self.title = title
25
+
26
+ def work(self):
27
+ print(self.title, 'の', self.name,'は働いていてる。')
28
+ ```
29
+ ```Python
30
+ # client.py
31
+ from buisiness_persion import BusinessPerson
32
+ bp = BusinessPerson('モミジ', 1.26, 24, 'freshman')
33
+ bp.bmi()
34
+ bp.work()
35
+ ```
36
+ 実行結果
37
+ ```
38
+ 15.11715797430083
39
+ freshman の モミジ は働いていてる。
40
+ ```