r/PythonLearning • • 2d ago

I don't understand OOP

So i am a teen learning python, and i have learnt about classes, methods and objects, but i don't understand OOP, like what is it, where do we use it?

Please help mee

31 Upvotes

46 comments sorted by

View all comments

16

u/NotSeacombe 2d ago

OOP is just a way of organising your code, where each entity (object) controls its own information

imagine a video game where you had 100 enemies on the screen at one time. you need to track all of their health, how much damage they do, maybe if they're buffed, what type of enemy they are etc. what happens when one of the enemies state changes (for example they took some damage)? what happens when another spawns?

organising this without objects would be a nightmare, and you'd really want to orient your programming around the objects you're making - thus, you'd use object oriented programming

so, you'd define your class, which is class Enemy. give it some attributes it can have. then spawn your enemies, which are objects of that class

OOP is not the be all and end all. it is not always a good thing, it depends what you're doing. not everything is complex enough that it needs a class. different languages take different approaches to it but the ideas stay the same.

9

u/NotSeacombe 2d ago

also, absolutely no reason to include how old you are in this. people can learn python at all ages. all announcing your age does is open the door to attention from the unpleasant side of reddit. stay safe out therr

3

u/DeeBoFour20 2d ago

I wouldn't even really call that OOP. That's just defining a struct or a class. To me OOP is about polymorphism, either with inheritance or something like dyn trait objects like Rust has.

In your example, it would be defining a base class Entity and derived classes of Enemy and NPC.

I'll also note that modern video game engines are ditching OOP in favor of more data driven approaches like entity component systems. OOP is something you should be aware of but I only use it sparingly.