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

1802 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Generate an array of integers from 0 to 99 and split it into two smaller arrays. Each smaller array will contain half of the original.


How do I use format python code?


Sales is real Customers is integer startmainprog input Sales input Customers if ((Sales > 500) OR ((Sales > 300) AND (Customers>9))) then output “Bonus Awarded” else output “No Bonus” end if end subroutine. Sales = 600 Customers = 9, answer:


What is the difference between a for loop and a while loop.


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