我尝试使用isinstance,但是得到的是错误的返回,而不是真实的。我的代码如下:
from CD import CD
from DVD import DVD
class Catalogo(object):
def __init__(self):
self.__itens = list()
self.cd = CD("ti", "te", "ar"," nu", "po", "co")
self.dvd = DVD("ti", "di", "te", "po", "co")
def appendItem(self, item):
self.__itens.append(item)
def listarItem(self):
print("CDs\n")
for i in self.__itens:
if isinstance(i, CD):
print(i,'\n')
else:
print(i + " is not here.")
print("DVDs\n")
for i in self.__itens:
if isinstance(i,DVD):
print(i,'\n')
#Another file
from catalogo import Catalogo
class main(object):
ct = Catalogo()
ct.appendItem(ct.cd.setArtista('Gilberto'))
ct.appendItem(ct.cd.setArtista('Zeca'))
ct.appendItem(ct.dvd.setDiretor('Carlos'))
ct.listarItem()
if __name__=="__main__":
main()
我试图通过其他值更改CD和DVD,但是代码不允许我这样做。我的问题是我该如何比较自己和自我。我的班级CD,DVD,Catalago和Main位于不同的文件中,我不放在此处,因为它们只是有结构。谢谢!!
答案 0 :(得分:0)
我只是回答。
from CD import CD
from DVD import DVD
class Catalogo(object):
def __init__(self):
self.__itens = list()
def appendItem(self, item):
self.__itens.append(item)
def listarItem(self):
print("CDs\n")
for i in self.__itens:
if isinstance(i, CD):
print(i,'\n')
else:
print(i + " is not here.")
print("DVDs\n")
for i in self.__itens:
if isinstance(i,DVD):
print(i,'\n')
#Another file
from catalogo import Catalogo
class main(object):
ct = Catalogo()
cd = CD("ti ", "te", "ar"," nu", "po", "co")
ct.appendItem(cd)
ct.listarItem()
if __name__=="__main__":
main()
我只是犯了一些错误,但我解决了这个错误,请遵循不同之处。