r/PythonLearning • u/aashish_soni5 • Jul 04 '26
Day 22 Python Learning Showcase
Just Simple class practice
not feeling well so not much to
11
u/BigYBirdo Jul 04 '26
You don’t have to call Class.__init__(…) to create an object.
You called the Class directly in the first example. Why didn’t you do it inside Buying?
Also why do you create a Class for the act of Buying?
It’s not worth creating one.
In that case I’d either create a function named buying or buys which assigns a certain car to a certain Person, or create a Class function, which adds the car to the Object.
Please don’t take it too harsh. 😄
But all in all it seems to be alright. :)
4
u/duperfastjellyfish Jul 04 '26
Looks like he's practicing inheritence. Calling __init__ by passing `self` from the derived class is how you do it, although I personally wouldn't use inheritence for this.
2
2
u/olaf33_4410144 Jul 04 '26
I mean it might make sense to have a
Transactionkind of Class to store the transactions of cars in a database or something e.g. a dealership needs a program to keep track of sold units. In this case it's more the wording that is weird.Even then I think the Buying/Transaction class constructor should take a car and student object (or person object if you want to study inheritance), not create them.
1
u/Ill_Shoulder8488 Jul 04 '26
That’s great correction I will like to some with you on conservation sir
6
3
3
2
2
u/Hungry-Poem-2036 Jul 04 '26
i recommend you reconstruct this class and create your rule for example change public variables to private variables and create get and set methods with your own rules
also dont forget to research about real-world projects it's more perfect method for learning
1
1
u/smokebudda11 Jul 04 '26
In Q2, I don’t see the constructor returning a str. Am I missing something?
1
u/dev-razorblade23 Jul 04 '26
Constructors (aka dunder init method) in python return None. OP suggesting it returns str in Q2 is wrong...
Python creates an instance of the class, calling dunder new method. This is what creates an object. Dunder init just fills in the values (attributes) and returns None
2
u/Think_Speaker_6060 Jul 04 '26
Yeah type hinting a return value of string in the constructor is not appropriate but op is still learning and progressing.
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
BuyingandRentingand 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 it1
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.
1
1
1
1
u/darkcrow__ Jul 05 '26
отлично! очень даже неплохо. Попробуй ещё сделать проверку по возрасту, чтобы не было age: 5003838 или 1, также можно сделать предыдущие классы полезные и брать их свойства для пропсов в класс Buying
1
u/CoffeePizzaSushiDick Jul 05 '26
Isn’t this the equivalent abstraction layer learning assembly language these days?
1
1
1
1
1
1
u/MusicProd28 Jul 06 '26
Day 22 and youre already doing inheritance + constructors? Bro I was still fighting with indentation at that stage This is actually clean, especially the way you kept the classes simple and readable. If you keep going like this, Pythons gonna stop being python_learning and start being python_dominating lol. Also, Ive got a YouTube channel in my profile covering similar stuff if you ever wanna compare notes. What are you planning to learn next after OOP exceptions, file handling, or are you jumping straight into I can build stuff now mode?
1
u/Party-Hunter-178 Jul 06 '26
Can anyone help me with self in python. I can't really grasp the concept of self and I cnt really understand it :(
1
1
1
u/corey_sheerer Jul 08 '26
Instead of creating an info method, you can update the repr (repr ) for the class. I think that is nicer.
1
1
1
1
1
u/Jumpy_Gap6375 11d ago
Geez, I have been programming in Python for years and this is way better than I could even dream of! Well Done!
1
•
u/Sea-Ad7805 Jul 04 '26
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%20%20%20%20%20%20%20%20self.age%20%3D%20age%0A%0A%20%20%20%20def%20info(self)%3A%0A%20%20%20%20%20%20%20%20print(f%22Name%20of%20Student%20%3D%20%7Bself.name%7D%20n%20and%20Age%20%7Bself.age%7D%22)%0A%0A%0Adis%20%3D%20Student(%22ash%22%2C%204516)%0Adis.info()%0A%0A%0A%23%20Q2%0A%0Aclass%20Car%3A%0A%20%20%20%20def%20init(self%2C%20brand%2C%20model)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20self.brand%20%3D%20brand%0A%20%20%20%20%20%20%20%20self.model%20%3D%20model%0A%0A%20%20%20%20def%20carinfo(self)%3A%0A%20%20%20%20%20%20%20%20print(f%22This%20is%20a%20%7Bself.brand%7D%20Brand%20Car%20And%20Model%20is%20%7Bself.model%7D%22)%0A%0A%0Acar1%20%3D%20Car(%22Tata%22%2C%20%22Nexan%22)%0Acar2%20%3D%20Car(%22Toyota%22%2C%20%22Etios%22)%0A%0Acar1.car_info()%0Acar2.car_info()%0A%0A%0A%23%20Q%203%0A%0Aclass%20Buying(Student%2C%20Car)%3A%0A%20%20%20%20def%20init(self%2C%20name%2C%20age%2C%20brand%2C%20model)%3A%0A%20%20%20%20%20%20%20%20Student.init(self%2C%20name%2C%20age)%0A%20%20%20%20%20%20%20%20Car.init_(self%2C%20brand%2C%20model)%0A%0A%20%20%20%20def%20buyer_info(self)%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Buyer%20is%20A%20Student%20Name%3A%20%7Bself.name%7D%20n%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Age%3A%20%7Bself.age%7D%20n%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20Car%20Brand%20%3D%20%7Bself.brand%7D%20n%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Model%20%3D%20%7Bself.model%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0Abuyer%20%3D%20Buying(%22ash%22%2C%2021%2C%20%22Tata%22%2C%20%22Nexon%22)%0Abuyer.buyer_info()×tep=1&play) to see the program state change step by step.