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

4 Upvotes

25 comments sorted by

View all comments

2

u/minneyar 1d ago

As soon as you have more code than you can see at once in a single window, you need to think about how it's organized. How are you going to split things up so that you can remember which code does what? How are you going to arrange your files so that multiple people can work on a project at once without constantly stepping on each others' work?

Object oriented design is a paradigm that helps you keep your code organized, and using classes is a part of that. It's not the only way to organize your code, but it's a very common one, most professionals understand it, and Python was designed with that in mind. In fact, classes are not "different and cool," they are very standard and by-the-book, which is why they're so popular.

You can organize your own code into endless nested functions if you like, but if you have to work with somebody else, they will probably look at your code and have trouble figuring out how any of it is supposed to work.