Object Oriented Programming (OOP) in Python

What we have learnt up to this point is functional programming, some programmers never use OOP and they can get along fine but to truly use Python to your advantage.

When you just start out and are writing simple programs, classes make a lot less sense. Why do you need a whole pets class so you can subclass to make a cat class that says ‘meow’ when you call the speak method? You don’t. You’re just doing it to learn subclassing and yeah, it’s a lot more work than just writing a cat_talk function. But it’s useful, because classes are all over the standard library and you’re going to need to know them to understand how to program python, even if you mostly write functional programs. In fact, most everything in python are objects, so knowing how classes work means you understand how these objects work better. See https://treyhunner.com/2019/04/is-it-a-class-or-a-function-its-a-callable/

If you are struggling with finding a place for classes in your own programs, start with dataclasses: https://docs.python.org/3/library/dataclasses.html I think you’ll find that you’ll soon be putting in helper methods into the dataclass, and the next thing you know, you’ll see why more complex classes makes sense.

functions inside a class have a special storage location they can share.

class instance can save variables across method use.

So if define a class for a ball.

class Ball:
def set_color(self, color):
you can create multiple balls each having it’s own color.

b1 = Ball()
b2 = Ball()
print(“Ball colors are:”, b1.color, b2.color)
without the class the function couldn’t operate easily on different objects.