Python Forum
Decorator is using in class,but not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorator is using in class,but not working
#1
# -*- 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())
Error:
PythonBetaTheNextLevelUpPart3_Decorator_Lambda_Ex2.py", line 81, in <module> print('factorial:',sci_cal.factorial()) TypeError: 'NoneType' object is not callable
Reply
#2
Your decorator extract_function_name does not need to be in class ScientificCalculator
and 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 
Reply
#3
(Jul-04-2020, 08:34 PM)Yoriz Wrote: Your decorator extract_function_name does not need to be in class ScientificCalculator
and 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 

Now is working , thanks your guidance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function-decorator , which is checking an access according to USERNAME Liki 6 624 Feb-17-2024, 03:36 AM
Last Post: deanhystad
  Decorator for a function with argument(s) banidjamali 1 1,860 Feb-09-2021, 11:55 AM
Last Post: Gribouillis
  i got a decorator question yosef 3 2,623 Aug-25-2020, 04:14 PM
Last Post: DeaD_EyE
  Decorator and namespace. JayIvhen 2 2,812 Oct-26-2018, 03:56 PM
Last Post: nilamo
  python decorator alfredocabrera 0 3,158 Feb-22-2017, 07:04 AM
Last Post: alfredocabrera

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020