lundi 11 février 2019

What is the clean way of checking if the paremeter is "None" in python? Inside of the function or when calling the function?

What is the clean way of checking whether the parameter is None or not?

Let's say I have three functions, and it is most likely that the parameters will have None values. func_1 and func_2 doesn't do anything if the parameters are None. They are like void functions without returning a value, just changing some settings.

def func_1(number):
    #do something with number

def func_2(number_2):
    #do something with number_2

def list_of_things(number=None, number_2=None):
    if number is not None:
        func_1(number)
    if number_2 is not None:
        func_2(number_2)

or

def func_1(number):
    if number_2 is not None:
        #do something with number

def func_2(number_2):
    if number_2 is not None:
       #do something with number_2

def list_of_things(number=None, number_2=None):
        func_1(number)
        func_2(number_2)

The second way looks better to me. However, I would like ask you which one is more readable, clean code?

Aucun commentaire:

Enregistrer un commentaire