モジュールをインポート

Python

カテゴリー: Data Structures  閲覧数:248 配信日:2018-02-15 11:20


インポート実行


作成したモジュール
・他のモジュールからimportを使ってインポートすることが出来る
import モジュール名

具体例
・a.pyをインポート
・拡張子の.pyは不要
import a      # モジュールをインポート

インポート後


クラスを使う場合
・モジュール名を先頭に付けクラス名をドットでつなげる
モジュール名.クラス名

メソッドを使う場合
・モジュール名を先頭に付けメソッド名をドットでつなげる
モジュール名.メソッド名

.変数を使う場合
・モジュール名を先頭に付け.変数名をドットでつなげる
モジュール名..変数名

具体例


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


▼index.py
import greeting_module      # モジュールをインポート

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



__main__ — トップレベルのスクリプト環境
モジュール (module)

週間人気ページランキング / 12-7 → 12-13
順位 ページタイトル抜粋 アクセス数
1 AttributeError: module 'sys' has no attribute 'setdefaultencoding' | AttributeError(エラー) 1
1 ゲストコメント一覧ページ 1
2025/12/14 1:02 更新