Python Forum
Getting Attribute Error In My Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Attribute Error In My Code
#1
this is my output before it breaks

tired from a long day of work
Margot



This is my error

Error:
Traceback (most recent call last): File "C:\Users\gabri\AppData\Local\Programs\Python\Python36\First Program.py", line 39, in <module> print(Margot.toString()) File "C:\Users\gabri\AppData\Local\Programs\Python\Python36\First Program.py", line 34, in toString 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) AttributeError: 'Student' object has no attribute '_Student__name' [hr]
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())
Reply
#2
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.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: audioio has no attribute 'AudioOut' netwrok 3 664 Oct-22-2023, 05:53 PM
Last Post: netwrok
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,402 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,289 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Attribute Error received not understood (Please Help) crocolicious 5 2,708 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  error in scapy attribute 'haslayer' evilcode1 5 6,562 Mar-02-2021, 11:19 AM
Last Post: evilcode1
  attribute error instead of correct output MaartenRo 2 2,210 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  attribute error stumped on how to fix it. hank4eva 7 4,791 Aug-11-2020, 04:47 AM
Last Post: hank4eva
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 2,024 Apr-29-2020, 07:10 PM
Last Post: deanhystad
  Attribute Error for Rename / Replace warden89 6 7,922 Jan-07-2020, 06:08 PM
Last Post: warden89
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 2,939 Sep-22-2019, 05:49 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020