Errors in the implementation of decorators in the Python programming language

39 Views Asked by At

In this program, I want to give a list of numbers including decimal and negative integers to the factorial function. Using a decorator, decimal and negative numbers are removed from the list, only integers remain, and the factorial of integers is returned as a list. But as you can see, in the end, nothing is returned as a result (None). Please advise where is the error. code:

def IntFactList(func):
    def func1(*args,**kwargs):
        IntList=[]
        for number in args:
            if number>0 and not isinstance(number,float):
                IntList.append(number) 
        print(IntList)                             
        func(*IntList,**kwargs)   
    return func1

    
@IntFactList
def Factorial(*args):
    factList=[]
    for number in args:
        fact=1
        while number>1:
            fact=fact*number
            number-=1
        factList.append(fact)
    return factList


list1=[2,3,4,5,3.5,5.8,-1,-3]
print(list1)
print(*list1)
print(Factorial(*list1))

result:

[2, 3, 4, 5, 3.5, 5.8, -1, -3]
2 3 4 5 3.5 5.8 -1 -3
[2, 3, 4, 5]
None
1

There are 1 best solutions below

0
ANUPAMA T.M On BEST ANSWER

The issue with the code lies in the decorator IntFactList. The decorator IntFactList modifies the behavior of the Factorial function by filtering out non-positive integers and floats from the input arguments. However, the modified Factorial function does not explicitly return a value, which causes it to implicitly return None. modify your code as below

def IntFactList(func):
    def func1(*args,**kwargs):
        IntList=[]
        for number in args:
            if number > 0 and not isinstance(number, float):
                IntList.append(number) 
        print(IntList)                             
        return func(*IntList, **kwargs)
    return func1