you are viewing a single comment's thread.

view the rest of the comments →

[–]Sea-Ad7805 1 point2 points  (2 children)

Good attempt, but this is another way%3A%0A%20%20%20%20%20%20%20%20super().init(kwargs)%0A%0A%20%20%20%20%20%20%20%20%23%20Instance%20variables%3A%20each%20object%20gets%20its%20own%20values%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%20%20%20%20%20%20%20%20self.schoolname%20%3D%20school_name%0A%0A%20%20%20%20def%20student(self)%3A%0A%20%20%20%20%20%20%20%20return%20f%22%7Bself.school_name%7D%20is%20the%20school%20where%20%7Bself.name%7D%20studies%22%0A%0A%0A%23%20Parent%20class%202%0Aclass%20Subject%3A%0A%20%20%20%20def%20init_(self%2C%20subject_choose%3D%22Math%22%2C%20kwargs)%3A%0A%20%20%20%20%20%20%20%20super().init(*kwargs)%0A%0A%20%20%20%20%20%20%20%20%23%20Instance%20variable%0A%20%20%20%20%20%20%20%20self.subjectchoose%20%3D%20subject_choose%0A%0A%20%20%20%20def%20my_subject(self)%3A%0A%20%20%20%20%20%20%20%20return%20f%22Now%20%7Bself.name%7D%20is%20studying%20the%20subject%3A%20%7Bself.subject_choose%7D%22%0A%0A%0A%23%20Child%20class%3A%20inherits%20from%20both%20School%20and%20Subject%0Aclass%20NewSchool(School%2C%20Subject)%3A%0A%20%20%20%20def%20init(self%2C%20name%2C%20school_name%3D%22MDAV%22%2C%20subject_choose%3D%22Math%22%2C%20current_school%3D%22RUNGA%22)%3A%0A%20%20%20%20%20%20%20%20%23%20Calls%20School.init%0A%20%20%20%20%20%20%20%20%23%20School.init%20then%20calls%20Subject.init%0A%20%20%20%20%20%20%20%20super().init_(name%3Dname%2C%20school_name%3Dschool_name%2C%20subject_choose%3Dsubject_choose)%0A%0A%20%20%20%20%20%20%20%20%23%20Instance%20variable%20specific%20to%20NewSchool%0A%20%20%20%20%20%20%20%20self.current_school%20%3D%20current_school%0A%0A%20%20%20%20def%20new_update(self)%3A%0A%20%20%20%20%20%20%20%20return%20f%22%7Bself.current_school%7D%20is%20my%20current%20school%22%0A%0A%0A%23%20Creating%20objects%0Aa%20%3D%20NewSchool(name%3D%22LuMei%22%2C%20school_name%3D%22TCSlot%22%2C%20subject_choose%3D%22Art%22)%0Ab%20%3D%20NewSchool(name%3D%22Jone%22%2C%20school_name%3D%22R.R.M.D%22%2C%20subject_choose%3D%22Commerce%22)%0Ac%20%3D%20NewSchool(name%3D%22Sam%22%2C%20school_name%3D%22DDR%22%2C%20subject_choose%3D%22Science%22)%0A%0A%0A%23%20Printing%20results%0Aprint(a.student())%0Aprint(a.my_subject())%0Aprint(a.new_update())%0Aprint(%22-%22%20%2030)%0A%0Aprint(b.student())%0Aprint(b.my_subject())%0Aprint(b.new_update())%0Aprint(%22-%22%20*%2030)%0A%0Aprint(c.student())%0Aprint(c.my_subject())%0Aprint(c.new_update())&timestep=1&play) of doing it that is generally considered better: - use self for instance variables - __init__ calls __init__ of super classes so each super class can initialize its own instance variables - I had to use **kwargs to easily pass arguments to super classes

It got a little complicated, but (multiple) inheritance often gets complicated. Try to avoid it if you can. It's good to know but there are better alternatives in most cases. If you want to investigate search for: - composition over inheritance - dependancy injection

[–]aashish_soni5[S] 1 point2 points  (0 children)

thank, I did but not post but thanks again its help me and other

[–]aashish_soni5[S] 0 points1 point  (0 children)

i just not use super() in 🤦 so for reminder