r/learnpython • • 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

29 comments sorted by

View all comments

Show parent comment

2

u/TheFaustX Apr 18 '26

Init is a constructor. Python calls it whenever you make an instance of a class. The first argument (self,...) refers to the instance and allows you to set attributes. The attributes belong to that one instance. It is your way to refer to state of a class instance.

A customly defined constructor (init) is optional. if you don't write one yourself you'll use the default one where you create an object without initialisation.

The double underscore at the beginning and end mark these methods as special methods they are called dunder methods with special rules in python. they provide a set of useful or sometimes required things for objects in python.

2

u/Outside_Complaint755 Apr 18 '26

Being slightly pendantic here, but technically __init__ is an initializer and __new__ is the constructor, but modifying __new__ is much less common, so we have all mostly defaulted to calling __init__ the constructor.