python class use function inside init code example
Example 1: 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
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s sq units" % (r.get_area()))
Example 2: python calling method from constructor
class TheBestClass:
def __init__(self, num1, num2)
self.number1 = num1
self.number2 = num2
self.AddTogether()
def AddTogether(self):
print(self.number1 + self.number2)