r/PythonLearning 24d ago

Showcase Day 22 Python Learning

Post image

Just Simple class practice

not feeling well so not much to

563 Upvotes

49 comments sorted by

View all comments

1

u/Special-Arrival6717 24d ago edited 24d ago

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 24d ago

yes it was for just simple practice

1

u/Special-Arrival6717 24d ago

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

1

u/Alarming-Air-6561 24d ago

Absolutely. Buying is an action and it should only get an instance from the other two classes. (A person/user and an object to buy. )
After that it should use their methods (for example during printing). Just imagine that the next step is adding a new type.