fromを使ってインポート

Python

カテゴリー: Data Structures  閲覧数:275 配信日:2018-02-19 10:07


fromを使ってインポート実行


クラスを作る度にモジュール名を記述するのが面倒だという場合、fromを使用
from モジュール名 import クラス名

fromを使ってインポートすると?


インポート後に呼び出す際、モジュール名を記述する必要がなくなる
・クラスを直接呼び出すことができる

具体例


▼greeting_module.py
class GreetingClass:
   def sayStr(self, str):
       print str


▼index.py
from greeting_module import GreetingClass

greeting  = GreetingClass()
greeting.sayStr("Hello")       # Hello


週間人気ページランキング / 12-12 → 12-18
順位 ページタイトル抜粋 アクセス数
1 _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?) | エラー 1
1 ゲストコメント一覧ページ 1
2025/12/19 1:02 更新