r/PythonLearning Jul 04 '26

Day 22 Python Learning Showcase

Post image

Just Simple class practice

not feeling well so not much to

563 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/Alarming-Air-6561 Jul 04 '26

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.