Python alership对象 汽车=汽车运输船() 选择=-1 #起动回路 而选择=0: 打印() 打印(“1.向经销商添加样车”) 打印(“2.在经销商处展示汽车”) 打印(“3.将汽车添加到经销商”) 打印(“4.从经销商处移除汽车”) 打印(“5.在经销商处更新汽车”) 打印(“6.出口汽车库存”) 打印(“0.退出程序”) choice=int(输入(“您的选择>>”) 打印() 如果选项==1: addSampleAutomobiles(汽车) elif选项==2: 汽车.打印汽车列表() elif选项==3: addAutomobile(汽车) elif选项==4: 移动汽车(汽车) elif选项==5: 更新汽车(汽车) elif选项==6: f=打开('AutomobileList.txt','w') f、 写入(str(汽车列表)) f、 关闭()

Python alership对象 汽车=汽车运输船() 选择=-1 #起动回路 而选择=0: 打印() 打印(“1.向经销商添加样车”) 打印(“2.在经销商处展示汽车”) 打印(“3.将汽车添加到经销商”) 打印(“4.从经销商处移除汽车”) 打印(“5.在经销商处更新汽车”) 打印(“6.出口汽车库存”) 打印(“0.退出程序”) choice=int(输入(“您的选择>>”) 打印() 如果选项==1: addSampleAutomobiles(汽车) elif选项==2: 汽车.打印汽车列表() elif选项==3: addAutomobile(汽车) elif选项==4: 移动汽车(汽车) elif选项==5: 更新汽车(汽车) elif选项==6: f=打开('AutomobileList.txt','w') f、 写入(str(汽车列表)) f、 关闭(),python,text,export,Python,Text,Export,您可以尝试在类中创建getter函数 def getAutoMobileList(self): return self.__automobilelist 之所以出现此错误,是因为automobilelist实际上没有在AutomobileAlership类的范围之外定义 如果您想从类中获取字段,应该调用.,在您的示例中,它是automobiles.\uu automobilelist 但在你的情况下,它仍然不起作用。当您在\uuu automobilelist前面加上双下划线时,您将其

您可以尝试在类中创建getter函数

def getAutoMobileList(self):
    return self.__automobilelist

之所以出现此错误,是因为automobilelist实际上没有在AutomobileAlership类的范围之外定义

如果您想从类中获取字段,应该调用.,在您的示例中,它是
automobiles.\uu automobilelist

但在你的情况下,它仍然不起作用。当您在\uuu automobilelist前面加上双下划线时,您将其定义为AutomobileAlership类的私有成员,因此您还应该为automobilelist创建一个getter函数(与getAutomobile一样)

可以在类中使用的简单getter函数:

def getAutoMobileList(self):
    return self.__automobilelist
然后调用
automobiles.getAutoMobileList()

另一种解决方法是: 您调用了
str(automobilelist)
,如上所述,它应该是
str(automobiles.getAutoMobileList())
,然后可以转换为
automobiles。这意味着您可以定义一个
\uuu str\uu
函数,而不是上面提到的getter函数。 因此,对于换行分隔值,您可以定义:

def __str__(self):
    return '\n'.join(str(i) for i in self.__automobilelist)

然后只需调用
str(automobiles)

您会遇到什么错误?NameError:未定义Automobilelist
Automobilelist
不存在。
automobiles.\uu automobilelist
有效吗?我得到了错误属性错误:“AutomobileAlership”对象没有属性“\uu automob ilelist”谢谢这非常有用!愚蠢的问题-它们应该显示在列表中还是文本文件看起来很混乱?我的文本文件看起来像[,,,]@SARAH我更新了答案,以便
\u str\u
函数可以打印一些有用的信息