我有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
函數調用或其他原因有關?
謝謝。
爲什麼要用'_l'導入模塊? –
這個錯誤很奇怪,但是如果沒有看到更多的代碼,特別是'find_threshold',就不可能知道發生了什麼。請發佈[mcve],以便我們重現此錯誤。 –
@折速我的錯誤。文件名也包含這個「_l」。 –