-1

keep getting 'RestaurantReview' object has no attribute '_RestaurantReview__name' but it seems as if I have passed everything into ReviewRestaurant from the Review class correctly

class Review:
    def __init__(self, nm, dt, strs):
        self.__name = nm
        self.__date = dt
        self.__stars = strs

    def set_name(self, name):
        self.__name = name
    def set_date(self, date):
        self.__date = date
    def set_stars(self,stars):
        self.__stars = stars
    def get_name(self):
        return self.__name
    def get_date(self):
        return self.__date
    def get_stars(self):
        return self.__stars
    def __str__(self):
        return 'RESTAURANT: ' + 'Name: ' + str(self.__name) + 'Date: ' + str(self.__date) + 'Number of stars: ' + str(self.__stars)

class RestaurantReview:
    def __init__(self, nm, dt, strs, csne):
        Review.__init__(self, nm, dt, strs)
        self.__cuisine = csne
    def set_cuisine(self, cuisine):
        self.__cuisine = cuisine
    def get_cuisine(self):
        return self.__cuisine

    def __str__(self):
        return 'RESTAURANT: ' + 'Name: ' + str(self.__name) + 'Date: ' + str(self.__date) + 'Number of stars: ' + str(self.__stars) + 'Cuisine' + str(self.__cuisine)


def main():
    r2 = RestaurantReview('Red Robin', '5-3-18', 4, 'Burgers')
    print(r2)

main()
  • Possible duplicate of [What is the meaning of a single- and a double-underscore before an object name?](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name) – CristiFati May 03 '18 at 14:40
  • 1
    Use just one *underscore* to name your fields: `self._name`. – CristiFati May 03 '18 at 14:50
  • all fields are supposed to have two underscores and be private – chris madden May 03 '18 at 14:51
  • 1
    `RestaurantReview` needs to be a subclass of `Review` for this to work. And if you insist on making your fields private, then you need to call your getter methods rather than trying to use `self.__name` in a different class than where that field was created. – jasonharper May 03 '18 at 14:54
  • Double underscore doesn't mean private. Python != Java. Is this an assignment? – juanpa.arrivillaga May 03 '18 at 16:14

1 Answers1

0

You need to declare RestaurantReview as a subclass of Review :

class Review:
    def __init__(self, nm, dt, strs):
        self.__name = nm
        self.__date = dt
        self.__stars = strs

    def __str__(self):
        return 'RESTAURANT: ' + 'Name: ' + str(self.name) + 'Date: ' + str(self.date) + 'Number of stars: ' + str(self.stars)

    @property
    def name(self):
        return self.__name

    @property
    def date(self):
        return self.__date

    @property
    def stars(self):
        return self.__stars

class RestaurantReview(Review):
    def __init__(self, nm, dt, strs, csne):
        super(RestaurantReview, self).__init__(nm, dt, strs)
        self.__cuisine = csne

    @property
    def cuisine(self):
        return self.__cuisine

    def __str__(self):
        return 'RESTAURANT: ' + 'Name: ' + str(self.name) + 'Date: ' + str(self.date) + 'Number of stars: ' + str(self.stars) + 'Cuisine' + str(self.cuisine)


def main():
    r2 = RestaurantReview('Red Robin', '5-3-18', 4, 'Burgers')
    print(r2)

main()

You don't need to privatise your attributes, otherwise you have to create getter methods to access each of your private attributes.

EDIT : I left the attributes as private, and added properties to get their values.

Kefeng91
  • 802
  • 6
  • 10
  • this works but the attributes need to be private for what I'm trying to do, i did forget setters upon submitting this which i have edited, but it still does not work – chris madden May 03 '18 at 15:20
  • I edited my answer so that your attributes are still private, but their values are accessible by the mean of properties. – Kefeng91 May 03 '18 at 15:28
  • The "double-underscore" prevents access to the attribute (except by getters), so it kind of emulates a private attribute. – Kefeng91 May 03 '18 at 16:23