__init__ method in Python


In this article, we explain the __init__ method in Python.

The __init__ method in Python is a method that allows us to initialize new instances of a class.

An instance of a class is new object created of the class.

So, for example, if we have a class named Student, an instance of this class may be Student1, Student2, Student3, etc.

But how do we know what attributes a new instance of a class contains?

Does it accept just the first name of the student? Does it accept the first name and last of the student? Does it accept the first name, last name, and age of the student? First name, last name, age, and grade of the student?

The __init__ method can be used to define exactly what attributes a new object of a class should have.

If you're coming from another OOP language such as Java or even OOP PHP, the __init__ method represents a constructor. A constructor is a method in a class that defines all attributes that a new object should be initialized with. If not, errors will occur.

So let's see how to use the __init__method in Python.

So let's create a class named Student.

Each new Student object should have contain the student's first name, last name, age, and grade.

We initialize these attributes in the __init__ method.

This is shown in the code below.

So in this code, we have created a class named Student.

In this Student class, we create an __init__ method, in which we define the attributes that all instances of this Student class have to contain (or else, an error will be thrown).

So like all methods, we create an __init__ method by using the keyword def before __init__

We then pass into the parameter of this __init__ method, self, firstname, lastname, age, grade.

When you create a method inside of a class, you always want to pass in self as the first argument. self should always be passed in as the first argument (or one of the arguments; it's good practice to make self the first argument of a method inside of a class). The reason this is done is because self is an argument that allows a new instance of a class to refer to itself. It's built-in Python keyword so that a newly created instance can refer to itself.

We then pass in firstname, lastname, age, and grade as the other parameters.

This means that all instances of the Student class must have the firstname, lastname, age, and grade specified. If they are not specified when the object is instantiated, then an error will be thrown.

We always use the format self.parameter= variable, because self.parameter is known, while the variable is unknown. Do not use the other way of putting, variable= self.parameter. This won't work.

So above we created a class called Student. We then created an __init__ method to initalize what attributes each new instance of the Student class must have.

We then created an object of the Student class, Student1.

Now if we call the attributes of the Student1 object, we get the following as output on our console.

So now you just a gist of how object instantiation works in Python.

Again, if you don't pass in all the necessary arguments into the __init__ method or you pass in too many arguments, an error will be thrown.

And this is how the __init__ method can be used to initialize instances of a class in Python.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...