fromを使ってインポート

Python

カテゴリー: Data Structures  閲覧数:291 配信日: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


週間人気ページランキング / 6-21 → 6-27
順位 ページタイトル抜粋 アクセス数
1 Python用語 16
2 インポート | インポートシステム 1
2 AttributeError: type object 'datetime.datetime' has no attribute 'datetime' | AttributeError(エラー) 1
2026/6/28 5:05 更新