Home > OS >  TypeError with default parameters in a function - Python
TypeError with default parameters in a function - Python

Time:07-08

Im trying to create a function that takes 6 arguments, 3 of which are "optional" given that I tried giving them default values. This function takes in the 6 values and gives them to another function (defined previously).

Every time I try to run it I get a TypeError: Coeff() got an unexpected keyword argument 'h'. I was hoping someone could help me with this!

def TSR_inf(coeff, maxmin, d, h='houle', t_h=0, Per=0):

   coeff = coeff
   maxmin = maxmin
   d=d
   h = h
   t_h = t_h
   Per = Per

   Coeff(coeff, maxmin, d, h='houle', t_h=0, Per=0)

The Coeff() function is defined as such:

def Coeff(coeff, maxmin, d, h='houle', t_h=0, Per=0):

  if coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 0 and Per == 0 and h == 'houle':
      Wave_max('x')
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 0 and Per == 0 and h == 'no houle':
      Wave_max('x', 'no houle')
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'reg' and Per == 0 and h == 'houle':
      Wave_max('x', 'reg')
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'reg' and Per == 1 and h == 'houle':
      Wave_max('x', 'reg', 1)
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'reg' and Per == 1.5 and h == 'houle':
      Wave_max('x', 'reg', 1.5)
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'reg' and Per == 2 and h == 'houle':
      Wave_max('x', 'reg', 2)
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'irreg' and Per == 0 and h == 'houle':
      Wave_max('x', 'reg')
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'irreg' and Per == 1 and h == 'houle':
      Wave_max('x', 'reg', 1)
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'irreg' and Per == 1.5 and h == 'houle':
      Wave_max('x', 'reg', 1.5)
  elif coeff == 'wave' and maxmin == 'max' and d == 'x' and t_h == 'irreg' and Per == 2 and h == 'houle':
      Wave_max('x', 'reg', 2)

This Coeff() function calls another function def Wave_max(d, h, t_h, Per): that I won't post here because it is irrelevant (but if you do want it let me know and I'll add it later).

If anyone can spot where I made the mistake that leads to the TypeError I won't be veery much appreciated!!

Cheers all! (and thank you in advance!)

CodePudding user response:

It's happening because the parameter name is h in the Coeff function and there is no parameter called h1 as the error rightly points out.

You probably want to do something like Coeff(coeff1, maxmin1, d1, h=h1, t_h=t_h1, Per=Per1) at the end of the TSR_inf function.

CodePudding user response:

when you pass an keyword argument always use the same argument name as used in the function definition, the issue is your function argument are h='houle', t_h=0, Per=0 and you are calling the function with h1='houle', t_h1=0, Per1=0

your actual function call should be:

Coeff(coeff1, maxmin1, d1, h='houle', t_h=0, Per=0)

or as you have already stroed them in variables

Coeff(coeff1, maxmin1, d1, h=h1, t_h=t_h1, Per=Per1)

and if you want to use h1 as keyword argument in Coeff function you can change your function definition as

 def  Coeff(coeff1, maxmin1, d1, h='houle', t_h=0, Per=0, **kwargs)

and use h1 in the function as

kwargs.get('h1')
  • Related