0

I'm trying to understand inheritance better. In the following code, when I try to print friend.salary, it throws an AttributeError. Doesn't WorkingStudent inherit all methods of the Student class?

class Student:
    def __init__(self,name,school):
        self.name = name
        self.school = school
        self.marks = []

    def average(self):
        return sum(self.marks)/len(self.marks)

    def friend(self,friend_name):
        return Student(friend_name, self.school)

anna = Student("Anna","MIT")
friend = anna.friend("Motilal")
#print (friend.name)
#print (friend.school)

class WorkingStudent(Student):
    def __init__(self,name,school,salary):
        super().__init__(self,name,school)
        self.salary = salary
        anna = WorkingStudent("Anna","SXS",25000)
anna.friend("Greg")
anna.marks.append(50)
print friend.salary 
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
learner
  • 9
  • 1
  • 3
    Is your indentation correct? Is that `anna = WorkingStudent(...)` line really supposed to be inside `__init__`? – BrenBarn Aug 05 '17 at 03:21
  • Yeah, it worked just fine with that indentation. I think that's the correct indentation. – learner Aug 05 '17 at 03:40
  • Possible duplicate of [Python derived class and base class attributes?](https://stackoverflow.com/questions/6396452/python-derived-class-and-base-class-attributes) – Clock Slave Aug 05 '17 at 05:13
  • Don't create a student in a method of another student, that is not their job. Students should be created by directly calling`motilal = Student("Greg", anna.school)` or `greg = WorkingStudent("Greg", anna.school)`. – Thierry Lathuille Aug 05 '17 at 11:22
  • I moved your problem statement before your code block. It's usually easier to digest code with some context. See [ask] for details. – ChrisGPT was on strike Aug 06 '17 at 21:09

1 Answers1

0

You should modify your source code as below

class Student:
    def __init__(self,name,school):
        self.name = name
        self.school = school
        self.marks = []

    def average(self):
        return sum(self.marks)/len(self.marks)

    def friend(self,friend_name):
        return Student(friend_name, self.school)


anna = Student("Anna","MIT")
friend = anna.friend("Motilal")
#print (friend.name)
#print (friend.school)


class WorkingStudent(Student):
    def __init__(self,name,school,salary):
        super(WorkingStudent,self).__init__(name,school)
        self.salary = salary
        # anna = WorkingStudent("Anna","SXS",25000)
    def friend(self,friend_name):
        return WorkingStudent(friend_name, self.school, self.salary)

# You should put your code here, because as your original code
# anna is an instance of Student not WorkingStudent class
# so it and its friend don't have "salary".
anna = WorkingStudent("Anna","SXS",25000) # After this line, anna is a different variable to "anna" variable that was defined before (anna = Student("Anna","MIT"))
friend = anna.friend("Greg") # friend now is an instance of WorkingStudent class, so it have salary
anna.marks.append(50)
print(friend.salary)

Editted. So code can work now

Toandd
  • 300
  • 1
  • 3
  • 13
  • Please test your code before answering, it doesn't even run - it doesn't correct the erroneous call to `super`. But anyway, changing the class of Anna won't make any difference, as Motilal still will be created as a `Student`, not a `WorkingStudent`, and so won't have a `salary` attribute. – Thierry Lathuille Aug 05 '17 at 11:13
  • It runs, but it makes no sense that a friend to a working student should be a working student, and a friend to a non working student a non working student. Creating new students this way is a *bad* idea, see my comment on the question. – Thierry Lathuille Aug 05 '17 at 12:00
  • Actually, I proposed a solution that help @Harish Rao Balaji understand the python Inheritance. And I can sure that create new students that way is NOT really BAD idea as you think. It is depend on each situation. I understand that Harish Rao Balaji want to create a student, then create all friends of that student, the purpose here is not create new student but create new friends of that student. I know that you can create new student, then you define another function to make both students become friend. But it depend on Harish Rao Balaji to do that work. – Toandd Aug 05 '17 at 12:30
  • Thank you Thierry Lathuille and Toandd, appreciate your time and feedback. The code was running but my concept and hence my application was not very clear. Now I understand it better! – learner Aug 05 '17 at 17:51