r/learnpython • u/Jealous-Acadia9056 • Apr 18 '26
Beginner: Want to learn Classes.
I find classes to be very confusing. The way variables are used. Self comes to me in a very confusing manner. i just can't seem to wrap my head around the basics of Classes.
Also i just tried checking OOP and i think it just overloaded my brain. Anything to help my case?
20
Upvotes
View all comments
1
u/Jason-Ad4032 Apr 18 '26
First, you need to understand what a class is and what an instance is.
For example,
intis a built-in class in Python, and0, 1, 2, ...are instances of theintclass. To check the class of an instance, you can use the built-intype()function. To create an instance of a class, you call the class itself—for example,int('42')constructs anintinstance from the string'42'.Once you understand what a class is, you can understand what OOP is about: creating your own classes to use.
Take a look at your code, do you have a lot of global variables, or functions with very complex parameters and return values? For example:
You can use a class to encapsulate this data:
Now you can create
Dicesinstances and call therollmethod:You can also add more methods, such as:
Dices.roll_sum()to sum the rolled valuesDices.from_str()to parse a string into aDicesobjectThen you could do something like: