eli44
eli44

Reputation: 150

can one python function argument depend on another argument?

I would like to have something like the following function:

calc_map(img_in, img_height, img_width, mapping = np.zeros((image_height, image_width)))
[function body]

apparently it is not possible that the default argument of mapping is depending on other arguments in that function. Is there another way around that without using image_height and image_width as global?

I would imagine to put this in my function:

calc_map(img_in, img_height, img_width)
mapping = np.zeros((image_height, image_width))
[function body]

but I also need to put an already existing mapping into the function and not having overwritten it..

appreciate any help, regards, eli

Upvotes: 0

Views: 593

Answers (2)

wasif
wasif

Reputation: 15470

Make default value to False and check:

def calc_map(img_in, img_height, img_width, mapping = False)
    if not mapping:
        mapping = np.zeros((image_height, image_width))

Upvotes: 0

Samwise
Samwise

Reputation: 71424

Make the default None in the function signature, and then replace it inside the body with whatever you want:

def calc_map(img_in, img_height, img_width, mapping = None)
    if mapping is None:
        mapping = np.zeros((image_height, image_width))

Upvotes: 2

Related Questions