What does __init__ mean?

There are more than one use for the expression init in Python.

If you see a file named init.py in a directory, it means that directory should be treated as a package (collections of variable, function and class definitions). This can be an empty file, or it can contain initialisation code for the package.

The more likely case you'll encounter it is inside a class, where init() initialises an instance of a class. You will see it defined like this

class MyClass:

    def init(self,a,b,c,d):

        #SOME CODE

What this means is that if you create an instance of a class: MyInstance = MyClass(a,b,c,d), this will run the init function, which will contain initialisation code. This defines what you do with your variables a,b,c,d. Note: self refers to the instance of the class being instantiated, and is implicit (you shouldn't include it when calling MyClass).

Some basic initialisation code could be:

self.a = a

self.b = b

etc.

CW
Answered by Callum W. Python tutor

1751 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Explain the difference between local and global variables


Explain why creating a list of several instances of one element using something like my_list = [a] * 5 can result in strange behaviour


What is the difference between a float and an integer variable type?


Describe both For-loops and While-loops and explain how you can simulate the effect of a for loop with a while loop with an example.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences