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.
Example 1 Create a class Calculator that can perform addition, subtraction, mutliplication, division.
Solution: Let's start with the Calculator class definition.
The example of program output is given below.
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.
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):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.
self.x = x
self.y = y
def addition(self):The third method is a subtraction of x and y attributes.
return self.x+self.y
def sub(self):The fourth method is the multiplication of x and y attributes.
return self.x - self.y
def mul(self):The fifth method is the division of x and y attributes.
self.x*self.y
def div(self):The code for the entire Calculator class.
return self.x/self.y
class Calculator: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.
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
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> 5The 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.
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
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