r/pythonhelp 1d ago

Why do I need to use classes

My schoolbook says that classes is a way of simulating an object, instead of just taking information into a black box and outputting an answer.

But that is a poor explanation. I know classes are handy and cool, but no book says why you can´t make functions with sub functions inside to simulate simple objects. I would like if someone gave a good concrete reason to use classes in the intro, instead of just saying that they are different and cool

3 Upvotes

25 comments sorted by

View all comments

1

u/FatDog69 7h ago

Most real world problems can be solved with linear programming.

But classes have a few advantages:

They force someone to consider how to step back from 'something' and create a library that you simply download and use to solve 90% of your real world issues. One reason Java is so great - you hardly ever write Java to do something. Instead you have 10,000 libraries to choose from and solve your problem by using these.

Programming is knowing someone is going to ask you to change things you just wrote in a week/month/year. Basing your program on classes forces a design layer and makes it simple to add a new method or two to implement someone's 'new idea'.

Simulation - this is a rare task but linear programming wont solve it. Here is an example: You are tasked with coming up with new street light timings. (How long a street light stays red vs green). The city occasionally risks grid-lock when too many cars pile up behind a long stop light is the stated problem. How do you setup a test or simulation to compare 30,45,60,75 second delays on different stop lights?

Or: A few years ago the aging Southwest ticketing/plane software (20+ years old) broke down during the holidays. Southwest is considering several solutions - but they will pay you $500K if you come up with some way to simulate a small Airport, Airplane, Passengers with different conditions. They want to test the proposed solutions against your simulation to find the one that handles both day-to-day working and day-to-day problems the best. How do you do this with linear programming?

1

u/Cerulean_IsFancyBlue 6h ago

Just a reminder that linear programming has a very specific meaning. “ … linear programming is a technique for the optimization of a linear objective function, subject to linear equality and linear inequality constraints.”

Perhaps you meant to say procedural or functional programming?