Jul-03-2020, 12:15 PM
# -*- coding: utf-8 -*- """ Created on Mon Jun 22 23:46:47 2020 @author: Lenovo """ import pdb import unittest import math class Calculator: """ A class is defined for calculator, to preform addition,subtration,multiplication,divison,power and exponent. """ def __init__(self,num1,num2): try: assert type(num1) == int assert type(num2) == int except AssertionError: print('Invalid parameter type') raise Exception self.num1 = num1 self.num2 = num2 def addition(self): #pdb.set_trace()# we have added a breakpoint here. The code pause execution here. #print(' Addition') return (self.num1 + self.num2) def subtraction(self): return(self.num1 - self.num2) def division(self): return(self.num1 / self.num2) def moduler(self): return(self.num1 // self.num2) def multiplication(self): return(self.num1 * self.num2) """def power(self): return(self.num1 ** self.num2) """ class ScientificCalculator(Calculator): #parent class refrence Calculator def __init__(self,num1,num2,number,exponent): #should be initialize this function __init__() super().__init__(num1,num2) # super() will refer paranent class variables self.number = number self.exponent = exponent def logg(self): #pdb.set_trace() #we have added a breakpoint here. The code pause execution here. return math.log(self.number,self.exponent) def power(self): a = self.number b = self.exponent return lambda a,b:a**b cal= Calculator(num1 = 2,num2 = 2) print('addition',cal.addition()) sci_cal = ScientificCalculator(num1=1,num2=2,number = 2,exponent = 3) print('log:',sci_cal.logg()) print('power:',sci_cal.power())
Output:addition 4
log: 0.6309297535714574
power: <function ScientificCalculator.power.<locals>.<lambda> at 0x0000011660DFE798>