Function fundamental: screen 9 - Functions with same name used

Screen Link: https://app.dataquest.io/m/315/functions%3A-fundamentals/9/reusability-and-multiple-parameters

Functions: Fundamentals Screen 9 has the below code where two functions with same name have been used.

def freq_table(index):
    frequency_table = {}
    
    for row in apps_data[1:]:
        value = row[index]
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1
            
    return frequency_table
# UPDATED FUNCTION
def freq_table(data_set, index):
    frequency_table = {}
    
    for row in data_set[1:]:
        value = row[index]
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1
    
    return frequency_table

My question is how does python know which function is to be executed in such cases. Is this done basis the parameters entered?

2 Likes

Python does not support method overloading so If you define two or more function with same names. Python will take latest implementation of function. So technically freq_table(index) does not exist if you define freq_table(data_set, index) later.

See more about python method overloading here

1 Like