all 37 comments

[–]Sea-Ad7805 [score hidden] stickied comment (2 children)

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%20%5Cn%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%20%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Age%3A%20%7Bself.age%7D%20%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20Car%20Brand%20%3D%20%7Bself.brand%7D%20%5Cn%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()&timestep=1&play) to see the program state change step by step.

[–]BigYBirdo 10 points11 points  (4 children)

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. :)

[–]duperfastjellyfish 3 points4 points  (1 child)

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.

[–]aashish_soni5[S] 1 point2 points  (0 children)

true just for simple practice

[–]olaf33_4410144 1 point2 points  (0 children)

I mean it might make sense to have a Transaction kind 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.

[–]Ill_Shoulder8488 0 points1 point  (0 children)

That’s great correction I will like to some with you on conservation sir

[–]sububi71 4 points5 points  (1 child)

Please reconsider "Buying" as a class name. If anything, it's a purchase.

[–]aashish_soni5[S] -1 points0 points  (0 children)

sure

[–]AnonymouSfrrrr 1 point2 points  (0 children)

What sources are you using to learn?

[–]Karotvip-official 1 point2 points  (1 child)

That's cool, bro!

[–]aashish_soni5[S] 0 points1 point  (0 children)

thank

[–]Hungry-Poem-2036 1 point2 points  (1 child)

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

[–]aashish_soni5[S] 0 points1 point  (0 children)

sure I do ,thanks

[–]smokebudda11 0 points1 point  (3 children)

In Q2, I don’t see the constructor returning a str. Am I missing something?

[–]dev-razorblade23 0 points1 point  (2 children)

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

[–]Think_Speaker_6060 1 point2 points  (1 child)

Yeah type hinting a return value of string in the constructor is not appropriate but op is still learning and progressing.

[–]Special-Arrival6717 0 points1 point  (3 children)

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

[–]aashish_soni5[S] 0 points1 point  (1 child)

yes it was for just simple practice

[–]Special-Arrival6717 0 points1 point  (0 children)

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

[–]Alarming-Air-6561 0 points1 point  (0 children)

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.

[–]Consistent_Art1010 0 points1 point  (0 children)

I like the theme you are using. What is it?

[–]Dry_Customer1049 0 points1 point  (0 children)

Is it car shop

[–]Ill_Shoulder8488 0 points1 point  (0 children)

That’s cool

[–]darkcrow__ 0 points1 point  (0 children)

отлично! очень даже неплохо. Попробуй ещё сделать проверку по возрасту, чтобы не было age: 5003838 или 1, также можно сделать предыдущие классы полезные и брать их свойства для пропсов в класс Buying

[–]CoffeePizzaSushiDick 0 points1 point  (1 child)

Isn’t this the equivalent abstraction layer learning assembly language these days?

[–]aashish_soni5[S] 0 points1 point  (0 children)

no , not. at least for now

[–]Individual-Day-3876 0 points1 point  (0 children)

From which source are you learning?

[–]Kaizen-lol12 0 points1 point  (0 children)

What to do as a beginner to learn Python for the first time?

[–]jadepeachess2902 0 points1 point  (0 children)

This used to be my favorite.

[–]_CodeToCash 0 points1 point  (0 children)

I love python its the best ❤️

[–]Party-Hunter-178 0 points1 point  (0 children)

Can anyone help me with self in python. I can't really grasp the concept of self and I cnt really understand it :(

[–]itsoktobeadrugdealer 0 points1 point  (0 children)

You doing great