【发布时间】:2020-12-19 23:24:18
【问题描述】:
我在第 26 行收到“AttributeError: 'str' object has no attribute 'model'”。我不知道为什么?不知道如何改正?
import time
import threading
def test(name,name2):
print(name)
print(name2)
car.show(name)
car.color(name2)
time.sleep(30)
class car():
def __init__(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model is", self.model )
print("color is", self.color )
audi = car("audi a4", "blue")
ferrari = car("ferrari 488", "green")
acura = car("acura" , "black")
BMW = car("BMW" , "blue")
Cadillac = car("Cadillac", "green")
f = open("/home/stuff/script/QT/car.txt", "r") #In car.txt file has car model and color list line by line
threads = []
for x in range (5):
name=(f.readline())
name=name.strip()
name2=(f.readline())
name2=name2.strip()
info = threading.Thread (target=test(name,name2))
threads.append(info)
info.start()
x= +x;
f.close()
【问题讨论】:
-
您需要使用正确的缩进正确地格式化您的代码。此外,提供完整的堆栈跟踪(完整的错误消息)。您还需要提供minimal reproducible example。目前您的代码中有很多似乎没有被使用。
-
好像你从来没有实例化过一个对象。您必须先执行此操作,然后才能对对象调用
show。
标签: python-3.x string class