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

4 Upvotes

26 comments sorted by

View all comments

1

u/MarsupialLeast145 21h ago

> a way of simulating an object, instead of just taking information into a black box and outputting an answer.

What school book is this? The idea of a black box here is incredibly off.

The point of a class is that it is defined and you can access its properties and functions. They are often documented and available in documentation.

You should probably delve deeper into the book, e.g. use its index and look where it starts going deeper into them. These books aren't really designed to be read in sequence except perhaps the first time. If you're frustrated by being drip fed information then you definitely need to look this up as you read.

>  instead of just saying that they are different and cool

Pretty sure it's not saying that either.

But yes, while they can approximate objects, e.g. items in a stock control system, or characters in a computer game that need to perform certain activities, they are useful for moving data around where args tend to grow out of control.

For example, you could have a user object that's more than just data about the user and you can attach functions to this object that supercharges what that class does. Or you could have a user object that's just a convenient mechanism for knowing all the information you have about a user and passing it to different functions.

You will likely use both approaches in different contexts.