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.

Answered by Callum W. Python tutor

1572 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Demonstrate a recursive solution to calculate the factorial of a number


Ask the user for a number and output the smallest divisor, bigger than one, for the inputted number. Output "Prime" if the number is a prime number.


Write a short program to print all the even numbers 1 to 100


How does a for loop work in Python?


We're here to help

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