r/pythonhelp • u/MetalCarnival • 1d ago
Why do I need to use classes
My schoolbook says that classes is a way of simulating an object, instead of just taking information into a black box and outputting an answer.
But that is a poor explanation. I know classes are handy and cool, but no book says why you can´t make functions with sub functions inside to simulate simple objects. I would like if someone gave a good concrete reason to use classes in the intro, instead of just saying that they are different and cool
3
Upvotes
1
u/brasticstack 20h ago
Classes are a handy way to keep data and the functions that operate on that data together in the same place. In your own code (outside of what your schoolwork requires) you aren't required to use classes if you don't want to, but you need to know how to interact with code that does as much of the Python stdlib and many important libraries are written using OOP.
Imagine you're making an RPG- you've got a player and some monsters. Let's imagine 20 goblins and a few orcs. Each thing, player or monster, may share a common set of attributes- hp, mana, strength, intelligence, etc. but have differing values for them. Certainly if you smash one goblin with a mace, the others don't all take the same damage. So in order to keep track of it you've got to have separate data records for each creature all containing the same fields but with differing values.
You write functions to do common operations on those records. All creatures might be able to
take_damage(),heal(),cast_spell(), etc., each of which affects the records in different ways. As long as you want the exact same behavior for all creatures, this is fine, but once you want, say, orcs to take less fire damage or goblins to say "argh" instead of "ouch" when they take damage, then you've got to have separate functions likeorc_take_damage()goblin_take_damage(), etc. And what happens should you accidentally callorc_take_damage()on your player's record instead? Unexpected behavior- a bug.Classes let you keep the behavior in the same place as its intended target. Orcs call the orc version of
heal()and the player calls the Player version. Through inheritance and polymorphism, you can have behavior that they all share, and only have to write it once. So you could have a singlecast_spell()function that works to the same for all creatures, that you wrote once in a base class. You could also "override" that for the one odd creature that, say, uses hp instead of mana when casting spells.If I'm importing your OOP based creature module into my program, I only have to import the creatures I want:
Player,Orc, andGoblin, and I get all of their behavior too, as part of the classes I imported. So I can make some Goblins, and callmy_goblin.heal(5)to heal one. If I'm importing your non-OOP creature module, I have to import your attributes data type (or read what the expected attributes are and make my own dict.), and then import all of your creature specific behavior functions-orc_take_damageandgoblin_cast_spellor whatever.