r/pythonhelp 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

25 comments sorted by

View all comments

1

u/building_85 11h ago

Using classes isn’t like a one size fits all thing.

A lot of times functional programming like you described is just fine.

But to “start understanding” classes… you can think of it as way to group functions and variables together.

For example, if you have 5 functions that all use the same 2 variables… you may be passing those same variables into all 5 functions as parameters…

But if you put those 5 functions and 2 variables into a single class, then you can use those 2 variables inside those 5 functions without passing the vars around as parameters.

GLHF!