Skip to content Skip to sidebar Skip to footer

Python : Optional Arguments For Function Calling Lower Level Functions

I have a function def weights(vector, loss_function, clipping, max_iterations=100, tolerance=1e-5) which needs to call a lower level loss function which can be any of these with

Solution 1:

defweights(vector, loss_function, clipping=None, 
            max_iterations=100, tolerance=1e-5)

kwargs = {}
if clipping:
    kwargs['clipping'] = clipping

huber_loss(vector, **kwargs)

Solution 2:

You can use max_iterations, tolerance and clipping as **kwargs and check for presence of keys inside arguments

defweights(vector, loss_function, **kwargs):
  if kwargs['max_iterations']:
    max_iterations = kwargs['max_iterations']
  else:
    max_iterations = 100
  ... # and so go on for clipping and tolerance

weights(vect, lf, maxa_iterations=5, clipping=2) 

you dont need to pass all kwargs that you check

PS. If you find an answer that is what you need - accept it :)

Post a Comment for "Python : Optional Arguments For Function Calling Lower Level Functions"