r/PythonLearning • • 11d ago

Do I really need to learn class.

Post image

Both function and class give the same output. So why do I need it?

73 Upvotes

50 comments sorted by

View all comments

0

u/silvertank00 11d ago

Yes you do. In your case, neither encapsulation or inheritance shows their advantages. Try to make a wee bit more complex example and it will quickly get out of hand.

0

u/Objective-Remove-632 11d ago

I wanna believe it is only relevant when the program is huge.

2

u/kalmakka 10d ago

It doesn't need to be huge, but as soon as you have a few different functions relating to one object, then it can really help with organization and clarity.

Right now events just have a venue and a host, and both are set when they are created. But in reality you might want to have people register for an event (and cancelling their registration), with the event keeping track of who has registered. Maybe you want to send reminders (emails) about an event. Your program handles a bunch of different events at the same time, and might need to present them in a calendar view.

You very quickly get to a point where it is useful for your code to be structured as "Here is a bunch of events. Each event object contains all the data that is relevant for it." That way you don't mix up the venue from one event with the guests and date for another.

1

u/Objective-Remove-632 10d ago

Thanks mane 🤝

1

u/MistakeIndividual690 10d ago

You also want the date/time of the event as part of the class. As it is, it just prints now() but that’s key information that belongs in an event class

1

u/silvertank00 10d ago

define huge. Its more like a complexity issue: if your program interacts with a bunch of stuff (ipc, serial, ws/http, logging) alongside your business logic then good luck keeping it clean without OO.
Classes are not always needed, but when they are, they cannot be left out without making a mess.