Getting Attribute Error In My Code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Getting Attribute Error In My Code (/thread-7128.html) |
Getting Attribute Error In My Code - Gabrbot180 - Dec-22-2017 this is my output before it breaks tired from a long day of work Margot This is my error
class Human:#Parent Class __name = None __age = None __feels = None def __init__(self, name, age, feels):#when creating a new human ur parameters pass through __init__ self.__name = name self.__age = age self.__feels = feels def set_name(self, name):#setters self.__name = name def set_age(self, age): self.__age = age def set_feels(self, feels): self.__feels = feels def get_name(self):#getters return self.__name def get_age(self): return self.__age def get_feels(self): return self.__feels def toString(self): return 'Hello I am {}, I am {} years old, and today I feel {}.'.format(self.__name, self.__age, self.__feels)#just another way of formatting text class Student(Human):#subclass/inherented class/child class __fav_class = None def __init__(self, name, age, feels, fav_class):#by default has all functions of its Parent class however you can overide them by redefining them self.__fav_class = fav_class super(Student, self).__init__(name, age, feels)#runs name age and feels through Human.__init__ def set_fav_class(self, fav_class): self.__fav_class = fav_class def get_fav_class(self): return self.__fav_class def toString(self): return 'Hello I am {}, I am {} years old, my favorite class is {}, and today I feel {}.'.format(self.__name, self.__age, self.__fav_class, self.__feels) Bob = Human('Bob', 32, 'tired from a long day of work') print(Bob.get_feels()) Margot = Student('Margot', 15, 'happy', 'Chem') print(Margot.get_name()) print(Margot.toString()) RE: Getting Attribute Error In My Code - Terafy - Dec-22-2017 The error is in the Student class def toString(self): return 'Hello I am {}, I am {} years old, my favorite class is {}, and today I feel {}.'.format(self.__name, self.__age, self.__fav_class, self.__feels)You cannot directly call members (self.__name, self.__age, self.__feels) from parent class (Human)... if that was your intentions. They must be passed using those getters methods you created in the Human class. |