python - 在python中一遍又一遍地得到这个“属性错误”
问题描述
谁能解释为什么这一直发生在我身上?
class TourAgency:
def __init__(self):
self._tours = {}
self._scheduledtours = {}
self._customers = {}
self._booking = {}
def addTour(self,code,tour):
self._tours[code] = tour
def addscheduledtours(self,code,scheduledtour):
self._scheduledtours[code] = scheduledtour
def addCustomer(self,code,customer):
self._customers[code] = customer
def addBooking(self,bookingId,booking):
self._booking[bookingId] = booking
def searchscheduledtours(self,code):
if code in self.scheduledtours.keys():
return self._scheduledtours[code]
else:
return None
mytour = TourAgency()
t1 = Tour("KO111","Discover Korea",8,7,1449.36)
print(t1)
ta = mytour.addTour('KO111',t1)
print(TourAgency.tours)
我收到一条错误消息:
打印(TourAgency.tours)
AttributeError:类型对象'TourAgency'没有属性'tours'
解决方案
您的班级没有tours属性,它只有_tours属性。也许您想改用它。
请记住,在 Python 中,如果属性名称以下划线开头,则意味着该属性应该是私有的,并且不打算由用户使用。
我希望这可以帮助你!干杯!
推荐阅读
- python - 由于“缺少”libssl 文件,Django/PostgreSQL 项目无法运行
- haskell - haskell 优雅的方式从无限的数字列表中过滤(减少)重复序列
- gitlab - 我可以将自托管 GitLab 配置文件中的统计信息导出到我的公共 GitLab 配置文件吗?
- python - 无法使用 set 过滤重复元素
- spring - MongoDB Springboot Query 注释只给出第一条记录
- tensorflow - Keras-二元多标签分类的加权二元交叉熵
- node.js - 如何在github上将客户端和服务器端项目文件夹作为一个项目(api +前端)一起推送?
- php - 选择下拉菜单始终显示最后一个选择
- azure - 如何为相同资源类型的多个资源创建不同的指标类型警报
- python - 如何解析 Airflow 中的嵌套宏