I’m doing the Python course and I have reached the section on classes. I understand we define classes so we can then define methods for classes for example. But why don’t we just use functions? I don’t really understand why we use classes and why we can’t just use functions for everything that classes do.
You’ve actually encountered a hot debate in the programming community on which is better: functional programming or object-oriented programming (OOP). OOP focuses on using classes, objects and methods to make the code run, whilst functional programming focuses on well, functions. There are benefits to both depending on how you like to program.
For example, storing properties and methods in an object can allow for a certain amount of self-referential behaviour, and good organisation. If you need to find something for a particular school, you know that the object for that school will contain the information you need. It also allows for inheritance from parent classes, meaning you can have many object instances, but only need to write a function once.
On the other hand, if you rely completely on functions you can name things very well and have a lot of small functions that do one specific task. Classes can have methods of course, but many people (including myself) find that to be a little clunkier. Also depending on the language the “this” keyword can behave a bit counter intuitively. With functional programming this issue doesn’t exist, you just have a function that does a task the same way every time, and you call it when it’s needed.
This is a bit controversial of a take I think, but both are completely fine. As long as you know what you are doing, you can generally get away with almost never using one or the other, depending on what your preferred language and projects are. It’s good to know both, but if you would rather just use functions, you go ahead and do that! Don’t let people tell you there’s a “right” way with using a class vs a couple functions, it’s mostly subjective.
So in Python why would we use classes over functions? Is it just to make organisation a bit cleaner and more legible in terms of grouping things? They just seem like functions, but used a bit more broadly. Like, we often have functions WITHIN the classes we define (as methods).
Classes have their place. As per my example of a school, if you have a school directory, each school has it’s own info however it also has common info with other schools. You could do this using functions and such however classes just work much better here. You can have common properties (eg. student count, class sizes, term time) however you can also have unique properties (eg. private schools have a cost to the parents, a specific school might have a lacrosse team while others do not).
When you’re dealing with real world objects, generally classes and class instances work much better than functions. These are things that aren’t impossible with functions, but just don’t work as well to model as a class does.