Skip to content

CLEANING: dictionary in constructor are usually not a good idea

That is not what you are suppose to do.

What do you want to achieve with a dictionary? Why not using keywords? If it is to avoid writing all the arguments when calling the function be aware that there are method for that:

class MySuperClass(object):
    '''This is a dummy class just for you
    
    Attributes:
    -----------
    dummy_member: float
        That is the main part of this class.

    '''

    def __init__(self, dummy_member, time_ten=False, multiplicator=None, power_of_two=True, *args, **kwargs):
        '''Initialize a new instance (=object) of MySuperClass

        Parameters:
        -----------
        dummy_member : float
            value of the dummy_member

        time_ten : bool, optional, default=False
            if set to True dummy_member value will be multiplied by 10

        multiplicator : float, optional, default=None
            if given multiplies the dummy_member value.

        power_of_two : bool, optional, default=True
            if True the given dummy_member value will be squared.

        '''
        self.dummy_member = dummy_member

        if time_ten:
            self.dummy_member = 10. * self.dummy_member

        if multiplicator is not None:
            self.dummy_member = multiplicator * self.dummy_member

        if power_of_two:
            self.dummy_member = self.dummy_member * self.dummy_member

This can then be called like:

planete_keywords = {'time_ten': True,
                    'multiplicator': 25.,
                    'power_of_two': True }

planete = MySuperClass(120., **planete_keywords)

the * and ** operators transform a tuple and a dict into a list of parameters and keywords respectively.

def function_with_params(a, b, c):
    ...
    return(d)

can be called:

function_with_params(1., 2., 3.)

or in the same way

params = (1., 2., 3.)
function_with_params(*params)

the same for keywords:

def function_with_keywords(a, b, kwa=None, kwb=True):
    ...
    return(w)

can be called like

function_with_keywords(2., 3., kwa=12., kwb=False)

or that way:

params = (2., 3.)
keywords = {'kwa': 12.,
            'kwb': False}
function_with_keywords(*params, **keywords)