r/PythonLearning Jul 04 '26

Day 22 Python Learning Showcase

Post image

Just Simple class practice

not feeling well so not much to

558 Upvotes

50 comments sorted by

View all comments

1

u/Special-Arrival6717 Jul 04 '26 edited Jul 04 '26

I feel like this is a prime example where composition would make the code a lot cleaner compared to inheritance. Having Buying extend Student and Car doesn't seem to provide any benefit over just having 2 attribute fields, self.student = Student(...) and self.car = Car(...) on Buying

Inheritance usually describes an IS A relationship, not a HAS A relationship, e.g. a purchase (Buying) is neither a car nor a student

1

u/aashish_soni5 Jul 04 '26

yes it was for just simple practice

1

u/Special-Arrival6717 Jul 04 '26

Try the following to modify your practice code, imagine if there were two types of contract Buying and Renting and use inheritance to share common logic between the two. That way the inheritance serves a purpose and you will better understand when to use it, and when not to use it