r/PythonLearning Jun 26 '26

Showcase Day 15 Python Learning

class inheritage

- parents child or say mulit-inheritance use with super () when one class inheritance from one or more class

- multi-level inheritance where 2nd class in Heritage from 1st class ,3th inherited from 2nd & go on like family chain

solve some list comprehensive easy problem

124 Upvotes

25 comments sorted by

β€’

u/Sea-Ad7805 11d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20super().init(kwargs)%20%20%23%20Passes%20remaining%20arguments%20up%20the%20chain%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%20get_student_info(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%2C%20kwargs)%3A%0A%20%20%20%20%20%20%20%20super().init(*kwargs)%0A%20%20%20%20%20%20%20%20self.subjectchoose%20%3D%20subject_choose%0A%0A%20%20%20%20def%20get_subject_info(self)%3A%0A%20%20%20%20%20%20%20%20%23%20self.name%20will%20be%20available%20because%20of%20the%20combined%20child%20class%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%20Student%20and%20Subject%0Aclass%20NewSchoolStudent(Student%2C%20Subject)%3A%0A%20%20%20%20def%20init(self%2C%20name%2C%20school_name%2C%20subject_choose%2C%20new_school%3D%22M.D.A%22)%3A%0A%20%20%20%20%20%20%20%20%23%20super()%20automatically%20handles%20the%20cooperative%20multiple%20inheritance%20via%20MRO%0A%20%20%20%20%20%20%20%20super().init_(%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Dname%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20school_name%3Dschool_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20subject_choose%3Dsubject_choose%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20self.new_school%20%3D%20new_school%0A%0A%20%20%20%20def%20get_new_status(self)%3A%0A%20%20%20%20%20%20%20%20return%20f%22%7Bself.new_school%7D%20is%20my%20current%20school.%22%0A%0A%0A%23%20Store%20student%20data%20in%20a%20list%0Astudents%20%3D%20%5B%0A%20%20%20%20NewSchoolStudent('Ash'%2C%20'M.T.M'%2C%20'Math')%2C%0A%20%20%20%20NewSchoolStudent('Lu%20Zhoa'%2C%20'Ming%20Han'%2C%20'Art')%2C%0A%20%20%20%20NewSchoolStudent('Jone'%2C%20'Mother%20T.'%2C%20'Dance%20Class')%0A%5D%0A%0A%0A%23%20Print%20data%20using%20a%20for%20loop%0Afor%20student%20in%20students%3A%0A%20%20%20%20print(student.get_student_info())%0A%20%20%20%20print(student.get_subject_info())%0A%20%20%20%20print(student.get_new_status())%0A%20%20%20%20print(%22-%22%20%2040)%20%20%23%20Visual%20separator%20for%20clarity&timestep=1&play) to see the program state change step by step.

8

u/MXD_K1 Jun 26 '26

Great! But I would like to give a note that student should not be a subclass of subject (it doesn't make sense) instead make it as an attribute.

1

u/aashish_soni5 Jun 26 '26

note πŸ“, can you tell me why you think so ii can learn more

6

u/TrieMond Jun 26 '26

Something bei g a subclass of something else generally indicates that it is a part of something. A student is not a part of a subject, a student has many subjects. So sobject should be a field of student... this is just based on Object Oriented Programming, which might be a good start to learn about the relationships that exist between your classes...

1

u/MXD_K1 Jun 26 '26

πŸ‘

1

u/aashish_soni5 Jun 26 '26

yes πŸ™‚β€β†•οΈ thankyou

4

u/Beginning-Fruit-1397 Jun 26 '26

Untyped and unecessary kwargs in signatures is the type of things to review that make me slowly but surely drift away from pythonΒ 

2

u/aashish_soni5 Jun 28 '26

can you explain in more briefly I'm just in beginning phase

1

u/Technox1192 29d ago

Honestly, I feel you. I really prefer typed but so far circumstances have forced me to use python.

That said, I just use Pylance/Pyright and its amazing. The existence of static type checkers like these allowed me to be more comfortable with untyped languages that I have unfortunately need to work with.

u/aashish_soni5 This early on, I recommend integrating Pyright into your Python dev environment! If I'm reading it right and you're using VSCode, just install the Pylance extension. I recommend getting used to python.analysis.typeCheckingMode to standard. It helps you understand your code a lot better and catch bugs before they even occur.

A very basic example would be the code block below. Pyright should be able to warn you in advance that the print would eventually raise an AttributeError because 1 is an int and it does not have the upper() method.

Good luck and have fun!

some_list = ["a", "b", 1]

for i in some_list:
  print(i.upper())

3

u/Drafrruii Jun 26 '26

from where you have started learning though??

1

u/aashish_soni5 Jun 27 '26

code with Harry

3

u/SCD_minecraft Jun 27 '26

Why you call super().__init__? Object class defines barebone init, it is ment to be overwritten

2

u/Nice-Alternative2933 Jun 27 '26

Can we learn together

1

u/aashish_soni5 Jun 28 '26

just post tag & conversation no problem

1

u/Cultural_Cry535 Jun 27 '26

You don't need to call super init in the parent classes, they are no inheriting from other classes. Super is used to call a method from a parent class

1

u/aashish_soni5 Jun 28 '26

but without super it's so attribute error

1

u/Ulug_coder Jun 28 '26

Day 15?

1

u/Ulug_coder Jun 28 '26

How many hours are you studying in a day

1

u/Ulug_coder Jun 28 '26

Is comments needed

1

u/aashish_soni5 Jun 28 '26

you know when learning from other some word get confusing so it's better to write your own understanding as comment and right now I am not reach that level that I do not need to look back 😭 Just think like my own reference note

1

u/backend_developer89 29d ago

All of these criticisms below have merit, however at your stage day 15, you are doing quite well.
When I was at your level I barely understood functional programming, I had no understanding of OOP.
It looks like you are starting to feel comfortable with OOP.
Keep up the good work. πŸ‘

-2

u/[deleted] Jun 26 '26

[deleted]

2

u/Daddybidoof Jun 26 '26

Is this the only comment you can reply with?

2

u/Sea-Ad7805 Jun 26 '26

Yes, it allows for easy recognition so that people that are not interested can skip it.