Monday, September 5, 2022

What is __init__ method ?

The __init__ method is called every time an object is created from a class. The __init__ method lets the class initialize the object attributes and it is only used within classes. The general form of __init__ method defined inside the class can be written in the following form.
class ClassName:
def __init__(self,var1,var2,...):
self.var1 = value
self.var2 = value

Example 1 Create a class Calculator that can perform addition, subtraction, mutliplication, division.
Solution: Let's start with the Calculator class definition.
class Calculator:
The body of the Calculator class will have in total five methods i.e. initialization method, addition, subtraction, multiplication, and division. The first method that will be defined is __init__ method.
    def __init__(self, x, y):
self.x = x
self.y = y
Using the __init__ method we have created a procedure for initializing object attributes x and y. The second method is the addition of the two attributes i.e. x and y., and the return value is the result of the addition.
    def addition(self):
return self.x+self.y
The third method is a subtraction of x and y attributes.
    def sub(self):
return self.x - self.y
The fourth method is the multiplication of x and y attributes.
    def mul(self):
self.x*self.y
The fifth method is the division of x and y attributes.
    def div(self):
return self.x/self.y
The code for the entire Calculator class.
class Calculator:
def __init__(self, x, y):
self.x = x
self.y = y
def addition(self):
return self.x+self.y
def sub(self):
return self.x - self.y
def mul(self):
self.x*self.y
def div(self):
return self.x/self.y
Now we will create a simple while loop in which the user will be asked to type two numbers and to choose the mathematical operation addition, subtraction, multiplication, or division. To exit the while loop user will have to type "quit". After typing the mathematical operation the result will be shown as output.
while True:
USER_INPUT1 = float(input("First number> "))
USER_INPUT2 = float(input("Second number> "))
res = Calculator(USER_INPUT1, USER_INPUT2)
print("""################################################################# # Choose the mathematical operation # 1. Addition => type add # 2. Subtraction => type sub # 3. Multiplication => type mul # 4. Division => type div # To terminate the program type quit #################################################################""")
USER_INPUT3 = input("Type mathematical operation>")
if USER_INPUT3 == "add":
print("{} + {} = {}".format(res.x, res.y, res.add()))
elif USER_INPUT3 == "sub":
print("{} - {} = {}".format(res.x, res.y, res.sub()))
elif USER_INPUT3 == "mul":
print("{}*{} = {}".format(res.x, res.y, res.mul()))
elif USER_INPUT3 == "div":
print("{}/{} = {}".format(res.x, res.y, res.div()))
elif USER_INPUT3 == "quit":
print("Program is terminated")
break

The example of program output is given below.
First number> 5
Second number> 10
#################################################################
# Choose the mathematical operation
# 1. Addition => type add
# 2. Subtraction => type sub
# 3. Multiplication => type mul
# 4. Division => type div
# To terminate the program type quit
#################################################################
Type mathematical operation>add
5.0 + 10.0 = 15.0
First number> 5
Second number> 10
#################################################################
# Choose the mathematical operation
# 1. Addition => type add
# 2. Subtraction => type sub
# 3. Multiplication => type mul
# 4. Division => type div
# To terminate the program type quit
#################################################################
Type mathematical operation>sub
5.0 - 10.0 = -5.0
First number> 5
Second number> 10
#################################################################
# Choose the mathematical operation
# 1. Addition => type add
# 2. Subtraction => type sub
# 3. Multiplication => type mul
# 4. Division => type div
# To terminate the program type quit
#################################################################
Type mathematical operation>mul
5.0*10.0 = 50.0
First number> 5
Second number> 10
#################################################################
# Choose the mathematical operation
# 1. Addition => type add
# 2. Subtraction => type sub
# 3. Multiplication => type mul
# 4. Division => type div
# To terminate the program type quit
#################################################################
Type mathematical operation>div
5.0/10.0 = 0.5
First number> 5
Second number> 10
#################################################################
# Choose the mathematical operation
# 1. Addition => type add
# 2. Subtraction => type sub
# 3. Multiplication => type mul
# 4. Division => type div
# To terminate the program type quit
#################################################################
Type mathematical operation>quit
Program is terminated
The most important part of this example is the __init__() method. This method is responsible for creating two attributes x and y each time the object was instantiated. As you can see in the while loop when object named res is created as the Calculator class instance two values are provided USER_Input1 and USER_INPUT2. These two values are numbers entered by the user and they are automatically assigned to class attributes x and y which are initialized using __init__ method.
After choosing the mathematical operation we want to apply to those two values (attributes) the method inside the class body is called and the mathematical operation is applied to those attribute values.

No comments:

Post a Comment