2017-04-01 25 views
0

我有一個主要的.py文件。如果我在一個主文件中包含2個文件,我可以從第一個包含文件的功能中獲得第二個

在此我導入2個其他文件。 第一個是包含一系列名爲debug.py的調試功能的模塊。 第二個只包含一個類定義。

我希望我的調試功能可以在類中調用。

我不想在我的類文件中導入debug.py,因爲它有可配置的選項,我不想在程序中多次設置。

這是可能的,我該怎麼做?

我在下面包含了一個非常簡化的代碼示例。

main.py:

import debug 
from class import CLASS 

debug.debug_messages_enabled = True 

my_object = CLASS() 

debug.py:

debug_messages_enabled = False 

def log (message): 
    if debug_messages_enabled: 
     output = "" 
     output += "[LOG]: " 
     output += message 
     print output 

class.py:

class CLASS (object): 
    def __init__(): 
     #I want to be able to access debug.log here 
+0

包括不是蟒蛇 – abccd

+0

你說得對,我的壞關鍵字。我對這門語言很陌生。我將編輯。 – Martha

回答

1

您需要導入debug.py在類文件。

main.py更改調試模塊中的一個設置並不相關。您的類文件將包含調試語句,進行調試調用等。

打印/不打印的決定將基於該設置進行。設置(debug_messages_enabled)將被更改main.py,但這與class.py無關。

class.py:

from debug import log 

class CLASS (object): 
    def __init__ (self): 
     log("A long thick section of trimmed, unhewn timber.") 
     self.foo = 1 
相關問題