What is __init__() in Python

Oct. 5, 2020, 7:08 a.m.

It is a function that is called automatically when an instance is created ,it is used to make assignments to instance attributes, self here refers to the instance. so here is a simple example


class Employee:
  def __init__(self, first_name, last_name):
    self.first_name = first_name
    self.last_name = last_name

emp1 = Employee("Erik", "Hersman")

print(emp1.first_name)
print(emp1.last_name) 

The output of the above code will be

Erik
Hersman

Resources

Keep Learning