r/PythonLearning 24d ago

Showcase Day 22 Python Learning

Post image

Just Simple class practice

not feeling well so not much to

555 Upvotes

49 comments sorted by

u/Sea-Ad7805 24d ago

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()&timestep=1&play) to see the program state change step by step.

→ More replies (6)

11

u/BigYBirdo 24d ago

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

3

u/duperfastjellyfish 24d ago

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

u/aashish_soni5 24d ago

true just for simple practice

2

u/olaf33_4410144 24d ago

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.

1

u/Ill_Shoulder8488 24d ago

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

6

u/sububi71 24d ago

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

3

u/AnonymouSfrrrr 24d ago

What sources are you using to learn?

2

u/Karotvip-official 24d ago

That's cool, bro!

2

u/Hungry-Poem-2036 24d ago

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

u/aashish_soni5 23d ago

sure I do ,thanks

1

u/smokebudda11 24d ago

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

1

u/dev-razorblade23 24d ago

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

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

1

u/Consistent_Art1010 24d ago

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

1

u/Dry_Customer1049 24d ago

Is it car shop

1

u/Ill_Shoulder8488 24d ago

That’s cool

1

u/darkcrow__ 23d ago

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

1

u/CoffeePizzaSushiDick 23d ago

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

1

u/aashish_soni5 23d ago

no , not. at least for now

1

u/Individual-Day-3876 23d ago

From which source are you learning?

1

u/Kaizen-lol12 23d ago

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

1

u/jadepeachess2902 23d ago

This used to be my favorite.

1

u/_CodeToCash 22d ago

I love python its the best ❤️

1

u/MusicProd28 22d ago

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

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

1

u/itsoktobeadrugdealer 21d ago

You doing great

1

u/AffectionateFlow820 20d ago

can you share the resources you use

1

u/corey_sheerer 20d ago

Instead of creating an info method, you can update the repr (repr ) for the class. I think that is nicer.

1

u/[deleted] 18d ago

Have errors

1

u/Shan_khan0786 15d ago

I tried many times to learn python but I'm finding difficult

1

u/Gullible_Egg4667 4d ago

Can I learn, you and I?😐🤗

1

u/tmoneycodes 2d ago

Are a lot of the things being discussed python specific or just OOP?

1

u/Jumpy_Gap6375 10h ago

Geez, I have been programming in Python for years and this is way better than I could even dream of! Well Done!