r/learnpython • • Oct 19 '26

I cant understand classes (nor self and __init__)

5 Upvotes

Hello if you wonder i switched from lua and luau to python. its going well i can make basically anything with native python libraries but i cannot understand what is a class, what does self mean what does __init__ mean. i recently understood what if __name__ == __main__ does but still i cannot understand classes. if you could give an example or explaining for me to understand i would be so grateful

r/learnpython • • Feb 17 '21

Please explain __init__ like ive never heard of programming before. Its just not making sense.

481 Upvotes

'''

class Student:

def __init__(self, name, major, gpa)

    self.name = name

    self.major = major

    self.gpa = gpa

student1 = Student("Bob", "Art", 3.0)

'''

Why do i need to use the init and self function? Do i have to make a new init for every single class?

Why cant i just make a regular function like

def Student(name, major, gpa) ??

r/learnpython • • Jun 09 '25

__init__.py - possibly a stupid question, but why..?

31 Upvotes

Obligatory caveat - complete newbie to Python - learning quickly.

I've got a python module supplied with a hardware device to use on a RaspberryPi - the instructions from the manufacturer involve setting up a venv and lots of complication, which I don't want to do as I'll be importing their module to my own existing code base. Their code comes in a directory (icm20948) with a __init__.py module and is called as normal by using [from icm20948 import ICM20948]

My understanding is that the presence of the __init__ makes Python treat the directory like a module, and I suppose I could rename the __init__ to icm20948.py and then call it the same way in my code.

What's the reason for the __init__ / directory structure, and is there an advantage in keeping it that way?

r/learnpython • • Oct 29 '25

Creating __init__.py files. Any tips?

4 Upvotes

Note: I just started learn Python about weeks ago to get into Python. First on my own then I bought Mr. Matthes' book - Python Crash course and I'm getting to point where you do the projects so I am going set a project directory structure with sub-directories for src, modules, classes, etc. But research online shows I need to those files at each directory so the code there knows where the other resources are. Thanks for any help.

r/learnpython • • Mar 10 '18

Can someone ELI5 what def __init__(self) means? I still don't get it :\

131 Upvotes

https://stackoverflow.com/questions/625083/python-init-and-self-what-do-they-do

Stack has a question just like this... But the explanations are insanely verbose and complex. I legit don't understand a word they're saying:

... the self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the A class and call its methods, it will be passed automatically...

Just... What? The instance of the object? I don't understand what this means.

The init method is roughly what represents a constructor in Python. When you call A() Python creates an object for you, and passes it as the first parameter to the init method

Again, don't understand. I don't know what a constructor is. But even if I did, I don't understand why it is passing the parameter to the init method.

Can anyone help elucidate what is going on?

r/learnpython • • Aug 30 '24

what can I put in __init__.py to make them useful

93 Upvotes

I recently learned (thanks wholly to others on this sub) how to organize my previously monolithic single .py file projects into modular directories (using poetry to make the project a package). I've been going at it all day, and this new world is very liberating and exciting!

But, now I have a bunch of empty __init__.py files in my project. Can I do anything useful with them? Do they work anything like class init, like 'execute everything in here first' when calling anything inside the same dir?

what can they be used for besides letting python know its a package structure?

r/learnpython • • Jan 07 '24

Whats the ruling on using __init__.py in 2024?

33 Upvotes

Every time I am having a module import problem, I come across someone mentioning init.py, only to be reminded by a (much later SO answer) that it's no longer needed in Python 3. I often don't know who to believe.

What's the final ruling the utility of init.py in 2024 greenfield projects (i.e. no legacy code)?