2017-06-10 46 views
0

我有3個文件。 find_threshold_l.py,simplelogicbuffer_l.py和test_find_threshold_l.py。導入一個類並將其用於另一個文件

我需要將前兩個文件導入最後一個文件。

這是simplelogicbuffer_l.py文件中的類:

class SimpleLogicBuffer: 

    def __init__(self, on_threshold): 
     self.on_threshold = on_threshold 
     self.output_state = False 

    def apply_input_voltage(self, value): 
     self.output_state = value > self.on_threshold 

    def is_on(self): 
     return self.output_state 

這裏是在find_threshold_l.py文件中的代碼:

from simplelogicbuffer_l import SimpleLogicBuffer 

def find_threshold(input_value, output_state): 

    if output_state == True: 
     while output_state == True: 
      input_value -= 10 
      if input_value > SimpleLogicBuffer.DUT_Logic_Buffer.on_threshold: 
       continue 
      else: 
       SimpleLogicBuffer.DUT_Logic_Buffer.output_state = False 
       input_value 
       return input_value/1000 
       break 
    else: 
     while output_state == False: 
      input_value += 10 
      if input_value <= SimpleLogicBuffer.DUT_Logic_Buffer.on_threshold: 
       continue 
      else: 
       SimpleLogicBuffer.DUT_Logic_Buffer.output_state = True 
       input_value -= 10 
       return input_value/1000 
       break 

這就是我在最後一個文件中寫道:

from simplelogicbuffer_l import SimpleLogicBuffer 
from find_threshold_l import find_threshold 

DUT_Logic_Buffer = SimpleLogicBuffer(12500) 

print("The threshold voltage is {}V".format(find_threshold(11000, DUT_Logic_Buffer.is_on()))) 

但是,當我運行此代碼時,出現錯誤。

它說:AttributeError: type object 'SimpleLogicBuffer' has no attribute 'DUT_Logic_Buffer'

我有點困惑。是否有與print函數調用或其他原因有關?

謝謝。

+2

爲什麼要用'_l'導入模塊? –

+0

這個錯誤很奇怪,但是如果沒有看到更多的代碼,特別是'find_threshold',就不可能知道發生了什麼。請發佈[mcve],以便我們重現此錯誤。 –

+0

@折速我的錯誤。文件名也包含這個「_l」。 –

回答

0

這會引發錯誤,因爲您的DUT_Logic_Buffer_1僅在test_find_threshold_l中可用,而您的SimpleLogicBuffer沒有任何屬性DUT_Logic_Buffer_1。您可以通過直接傳遞函數find_threshold中的值而不是SimpleLogicBuffer.DUT_Logic_Buffer_1.on_threshold來解決此錯誤,或者您可以直接執行下列操作:SimpleLogicBuffer(some_value).on_threshold

+0

謝謝。無論如何,我可以初始化test_find_threshold.py文件中的DUT_Logic_Buffer_1? –

+0

將實例作爲參數傳遞。 – SH7890

相關問題