I\'m new to python and programming in general, so would really appreciate any clarification on this point.
For example, in the following code:
#
This is a more general answer than for just your code, but classes are useful when you need to:
Share state safely across specific functions.
Classes offer encapsulation to avoid polluting the global namespace with variables that only need to be shared across a few functions. You don't want someone using your code to overwrite a variable all of your functions are using - you always want to be able to control access as much as possible.
Classes can be instantiated and have multiple independent copies of the same variable that are not shared at all while requiring very little code use.
They're appropriate when you need to model interactions between a collection of functions and variables with another collection of functions and variables i.e. when you need to model objects. Defining a class for a single function with no state is frankly a waste of code.
Provide operator overloading or define specific traits for certain types
What makes int, string, and tuples hashable (can be used as keys in a dictionary or inserted into a set), while lists and dicts aren't?
Answer: the base classes for these primitive types implement the magic method __hash__, which enables the Python compiler to dynamically compute a hash at runtime.
What allows the + operator to be overloaded, so that 5 + 2 = 7 for ints but 's' + 'a' = 'sa' for strings?
Answer: the base classes for these types defines their own __add__ method, which lets you define exactly how addition between two members of your class is carried out.
All user-defined classes can implement these methods. There are good use cases for them: larger mathematical libraries - numpy, scipy, and pandas - make heavy use of operator overloading by defining these magic methods to provide you useful objects to handle your data. Classes are the only way to implement these traits and properties in Python.
Classes are not superior to functions when the above conditions are not met.
Use a class to group together meaningful functions and variables in a sensible way, or to endow your objects with special properties. Anything else is superfluous.