Usage of classes

Classes are great, but in my personal opinion, reusability and encapsulation are the worst arguments for them - because you can do that with packages, modules, functions and decorators just fine.

I think complex data handling and states are the best reason to use classes. Examples:

  1. You download big files - your function returns a Download() instance, which holds information about its current status. You can do cancel() and other methods.
  2. You want a Clipboard() which can be sorted, added to another one, compared to other clipboards etc.

    These are two cases where I used classes recently, and it makes perfect sense. Unfortunately, most introduction go with

    class Animal: def init_(self):

    class Dog(Animal):

Which is a really bad example because it's totally unnecessary. Unless, for example, your dog's have useful dunder methods or any other reason for being a class.

/r/learnpython Thread