Skip to content Skip to sidebar Skip to footer

Python Code Is Not Throwing Error But The Desired Output Is Not The Same

Unable to receive the output of the python code.I have tried debugging the code by printing each line def get_sum_metrics(predictions, metrics=[]): for i in range(0,3):

Solution 1:

Your issue here is related to mutable default arguments and the problem of creating a lambda in a loop as shown in this question

Those two things fixed gives you:

def get_sum_metrics(predictions, metrics=None):
    if metrics is None:
        metrics = []  
    for i in range(0,3):
        f = lambda x, i=i: x+i
        metrics.append(f)
    sum_metrics = 0
    for metric in metrics:
        sum_metrics += metric(predictions)
    return sum_metrics

Post a Comment for "Python Code Is Not Throwing Error But The Desired Output Is Not The Same"