Decorator is using in class,but not working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Decorator is using in class,but not working (/thread-28093.html) |
Decorator is using in class,but not working - mbilalshafiq - Jul-04-2020 # -*- 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): #should be initialize this function __init__() super().__init__(num1,num2) # super() will refer paranent class variables self.num1 = num1 self.num2 = num2 def logg(self): #pdb.set_trace() #we have added a breakpoint here. The code pause execution here. return math.log(self.num1,self.num2) def power(self): return (lambda a,b:a**b)(self.num1,self.num2) def extract_function_name(func): def internal_method(*args,**kwargs): print('the method called is:',func.__name__) returned_value = func(*args,**kwargs) print('the method execution is complete') return returned_value #adding decorator to the function @extract_function_name def factorial(self): #return (lambda a:a) ([self.num1 if self.num1 == 0 self.num1 = 1 else self.num1 * factorial(self.num1 - 1)]) return (lambda a:a) (math.factorial(self.num1)) cal= Calculator(num1 = 2,num2 = 2) print('addition',cal.addition()) print('subtraction',cal.subtraction()) print('division',cal.division()) print('multiplication',cal.multiplication()) print('moduler',cal.moduler()) sci_cal = ScientificCalculator(4,3) print('log:',sci_cal.logg()) print('power:',sci_cal.power()) print('factorial:',sci_cal.factorial())
RE: Decorator is using in class,but not working - Yoriz - Jul-04-2020 Your decorator extract_function_name does not need to be in class ScientificCalculatorand it needs to return internal_method , at the moment it defaults to returning None .def extract_function_name(func): def internal_method(*args,**kwargs): print('the method called is:',func.__name__) returned_value = func(*args,**kwargs) print('the method execution is complete') return returned_value return internal_method RE: Decorator is using in class,but not working - mbilalshafiq - Jul-04-2020 (Jul-04-2020, 08:34 PM)Yoriz Wrote: Your decorator Now is working , thanks your guidance. |