mardi 16 avril 2019

Is it a good practice to have a function call itself with different arguments to implement function overloading?

I know there's no function overloading in python, so people in general just inspect the arguments, and create the logic inside a single function.

I have a function that does something if you give a set of arguments (called below pre_processed_params). If I give other arguments (with less pre-processing, called below raw_params) I would like to call the same function. So I can execute the function even when I provide the raw arguments (no pre-processing), and then do the pre-procesing inside the function.

EDIT:

I provide below an example that illustrates this:

def preprocess(raw_params):
    # do some preprocessing
    return pre_processed_data


def calc_result(raw_params=None, pre_processed_params=None):
    if raw_params:
        pre_processed_args = preprocess(raw_params)
        result = calc_result(pre_processed_params=pre_processed_args)
        return result
    elif pre_processed_params:
        # do some calculation with pre_processed data and create the result 
        return result
    else:
        raise Exception('wrong parameters')

The actual motivation to this question is a code I am developing to generate occupation maps of an animal tracked by a camera. So I want to have a function generate_occupation_map that can either receive the raw tracking data or a preprocessed data (width of the cage normalized to 1, positions starting at the origin and rotated so that the cage is aligned with x and y axes)

Is using this recursive call deemed as acceptable/good practice in this case? Can you provide examples from standard libraries that do something like that?

Aucun commentaire:

Enregistrer un commentaire