2017-06-21 3 views
1

別のファイルのオブジェクト内の1つのスクリプトの変数を使用する良い方法はありますか?私はグローバルを使うことはうまくいくと思ったが、そうはしなかった。2番目のオブジェクトで1つのスクリプトの変数を使用する最もPythonの方法

私の設定読み込みクラスには、設定ファイルをディスクから辞書に読み込む高価なディスク操作があります。当然、私は設定変数が必要なたびにこの操作を繰り返さないようにしたい。私は他のすべてのオブジェクトが使用するためにそれを記憶しておきたい。

これは機能しますが、不安定です。 battery.pyとは別に、私は他のクラスを表示せず、すべての人のためのパラメータとしてconfigを渡す必要があります。

main.py:

#!/usr/bin/python 
from config import CONFIG 
from battery import BATTERY 
config = CONFIG().config 
battery = BATTERY(config) 
print("Running this the first time.") 
battery.print_config() 
print("Running again. Should not go slow.") 
battery.print_config() 

config.py:

class CONFIG(object): 
    def __init__(self): 
     # Simulating the expensive disk operation 
     from time import sleep 
     for i in range(1, 4): 
      print i 
      sleep(1) 

     self.config = {'parameter': 'value'} 

battery.py

class BATTERY(object): 
    def __init__(self, config): 
     self.config = config 

    def print_config(self): 
     print(self.config['parameter']) 

結果:

$ ./main.py 
1 
2 
3 
Running this the first time. 
value 
Running again. Should not go slow. 
value 

私はbattery.pyのinitの中でグローバル設定しようと思ったが、それは以下のエラーで失敗する。

class BATTERY(object): 
    def __init__(self): # Removed the config argument 
     # Added global config 
     global config 
     self.config = config 

    def print_config(self): 
     print(self.config['parameter']) 

結果:

$ ./main.py 
Traceback (most recent call last): 
    File "./main.py", line 5, in <module> 
    battery = BATTERY() 
    File "/cygdrive/c/Users/nbktvlh/Desktop/Test/battery.py", line 4, in __init__ 
    self.config = config 
NameError: global name 'config' is not defined 

これは動作しますが、それがより緊密に結合します。 (右?)そして不思議なことに、それはプリントを2回実行します。

class BATTERY(object): 
    def __init__(self): 
     # Added this line 
     from main import config 
     self.config = config 

    def print_config(self): 
     print(self.config['parameter']) 

結果:

$ ./main.py 
1 
2 
3 
1 
2 
3 
Running this the first time. 
value 
Running again. Should not go slow. 
value 
Running this the first time. 
value 
Running again. Should not go slow. 
value 

多分これを行うには良い方法がありますか?

+0

remov e configクラス、そして単に 'from config import config'? –

+1

また、Pythonには真のグローバル変数はありません。 「グローバル」スコープは実際には「モジュールレベル」のスコープを意味します。 –

+0

@ juanpa.arrivillaga設定クラスを削除することを説明してください。 configファイルがインポートされるたびに読み込まれることはありますか? – SlowBro

答えて

0

@ juanpa.arrivillagaのおかげで答えが出ました。私は設定をクラスにするべきではありません。

main.py:

#!/usr/bin/python 
from config import config 
from battery import BATTERY 
battery = BATTERY() 
print("Running this the first time.") 
battery.print_config() 
print("Running again. Should not go slow.") 
battery.print_config() 

config.py:

# Simulating the expensive disk operation 
from time import sleep 
for i in range(1, 4): 
    print i 
    sleep(1) 

config = {'parameter': 'value'} 

battery.py:

class BATTERY(object): 
    def __init__(self): 
     from config import config 
     self.config = config 

    def print_config(self): 
     print(self.config['parameter']) 

結果:

$ ./main.py 
1 
2 
3 
Running this the first time. 
value 
Running again. Should not go slow. 
value 
関連する問題