首页 > 解决方案 > 在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'

标签: pythonclassattributes

解决方案


您的班级没有tours属性,它只有_tours属性。也许您想改用它。

请记住,在 Python 中,如果属性名称以下划线开头,则意味着该属性应该是私有的,并且不打算由用户使用

我希望这可以帮助你!干杯!


推荐阅读