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

1781 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

How can you take a list of numbers and add 3 to every number in the list using only one line of code?


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10


Write a simple Python program that asks a user to input their age and name and then return the text: "Hi my name is *NAME*, and I am *AGE* years old."


How do you write code to implement a recursive fibonacci algorithm?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences