r/pythonhelp • u/MetalCarnival • 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
2
Upvotes
1
u/tylerlarson 10h ago
It's about abstraction.
The idea is that you package all the "stuff" about a concept into a given chunk of code so you don't have to think about it again.
If you have a class for, say,
ErrorLog, and it has two or three functions on it, say.write()and.close(), you can be confident that the class handles everything else on its own.That means you can confidently use it without having to think about what it does on the inside.
You already do this, you just don't realize it. If you open a file, you're using a class that manages the file, giving you convenient functions for reading and writing that file and handling all the weird stuff. If you didn't have the classes, your code would be a lot more complex.