init method in python class code example

Example 1: self and init in python

class Computer:
    def __init__(self):
        self.name= ("Pankaj")
        self.age= 28
c1=Computer()
c2=Computer()
c1.name= "Garg"
print(c1.name)
print(c2.name)

Example 2: what is __init__ in python

# If you are familiar with C++ or Java think of the __init__ method as a constructor.
# It is the method that is being called when the class is called.In the following
# example we will see how we can call the __init__ method

my_variable = MyClass()

Example 3: init function in python

class Rectangle:
   def __init__(self, length, breadth, unit_cost=0):
       self.length = length
       self.breadth = breadth
       self.unit_cost = unit_cost
   def get_area(self):
       return self.length * self.breadth
   def calculate_cost(self):
       area = self.get_area()
       return area * self.unit_cost
# breadth = 120 units, length = 160 units, 1 sq unit cost = Rs 2000
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s sq units" % (r.get_area()))