Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
class Grandfather:
    last = 'wilson'
    citizenship = 'american'
    residence = 'Detroit'
    def __init__(self,first,college,age,career,workplace):
        self.first = first
        self.college = college
        self.age = age
        self.career = career
        self.workplace = workplace
        self.email = first + '-' + Grandfather.last + '@email.com'
    @property
    def fullname(self):
        return '{} {}'.format(self.first,Grandfather.last)
    def __str__(self):
        return '{} {}'.format(self.first,self.last)
    @property
    def history(self):
       return 'my name is : {}, i graduated from {} and work as {} in {}'.format\
           (self.fullname,self.college,self.career,self.workplace)
    @classmethod
    def set_residence(cls,newR):
        cls.residence = newR
    @classmethod
    def set_cit(cls,new_cit):
        cls.citizenship = new_cit
    



class Father(Grandfather):
    def __init__(self,first,college,age,career,workplace,sport,wife):
        super().__init__(first,college,age,career,workplace)
        self.sport = sport
        self.wife = wife


class Son(Father):
    def __init__(self,first,college,age,career,workplace,sport,wife,Vedio_Game,Car):
        super().__init__(self,first,college,age,career,workplace,sport,wife)
        self.Vedio_Game = Vedio_Game
        self.Car = Car





john = Grandfather('john','stanford',70,'engineer','Ford')
mark = Father('mark','MIT',40,'CScientist','SONY','Tennis','Sara')
Maik = Son('Maik','MIT',26,'SoftwareEngineer','Google','tennis','caro','MineCraft','Toyota')
if __name__ == '__main__':
    print(john.fullname)
    print(john)
    print(john.history)
    Grandfather.set_residence('Frankfurt')
    print(Grandfather.residence)
    print('############')
    print(mark.fullname)
    print(mark)
    print(mark.history)
    print(mark.residence)
    print('###########')
    
    print(Maik.fullname)
    print(Maik)
    print(Maik.residence)


What I have tried:

adding all existing attributes in Father and Grandfather classes to the son class

after the python file has been executed it throws the error:
Traceback (most recent call last):
  File "my.py", line 57, in <module>
    Maik = Son('Maik','MIT',26,'SoftwareEngineer','Google','tennis','caro','MineCraft','Toyota')
  File "my.py", line 40, in __init__
    super().__init__(self,first,college,age,career,workplace,sport,wife)
TypeError: __init__() takes 8 positional arguments but 9 were given
Posted
Updated 17-Jun-20 5:23am
v6
Comments
Richard MacCutchan 17-Jun-20 10:29am    
What is the question?

1 solution

You do not need to pass the self reference to the super class as:
Python
        super().__init__(self,first,college,age,career,workplace,sport,wife)

#
# just use
#
        super().__init__(first,college,age,career,workplace,sport,wife)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900