LoginSignup
2
1

More than 5 years have passed since last update.

pythonでimport元のclass内変数に引数を渡す

Last updated at Posted at 2018-07-06

はじめに

2018/7/9修正
私の理解不足でした。修正しました。

もともとはログ出力モジュールを作って、複数のスクリプトでそれを読み出し。
ログファイル名は読みだした側のスクリプト名にしたり、ログディレクトリをスクリプト毎に分けたりしたかった。

/jikkou.py
/lib/module.py

test.pyが親スクリプト的なもので、module.pyの中身を読み込んで流用する。
この時にtest.pyで、module.pyの__init__とstdout関数に引数を渡したい。

それぞれの中身

jikkou.py

#!/usr/bin/python3 -B
# -*- coding: utf-8 -*-

from lib import module

# def main()
def main():
        module.output('hoge').stdout('foo')

# exec main()
if __name__ == '__main__':
        main()

module.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-

class output:
    def __init__(self, out1):
        print(out1)

    def stdout(self, out):
        print(out)

実行結果

./jikkou.py
hoge
foo

module.output('hoge').stdout('foo')
init側とclass内関数側に引数を渡して実行する。

2
1
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1