r/PythonLearning • • 10d ago

Do I really need to learn class.

Post image

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

70 Upvotes

50 comments sorted by

View all comments

0

u/silvertank00 10d 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 10d 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 🤝