Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
**kwargs doesn't pass variables to the function, what's wrong? [double]

For some reason, I can't pass variables in kwargs in Python for the receiving function to see. (SOLVED - see edit). This illustrates the problem: kwargs parameters are passed through various functions and arrive at the integrand function. I put a print (kwargs) in there that shows the entire dictionary I created in the first few lines below. BUT these variables are not seen by the integrand function and an error is thrown. What is the correct way to make these variables accessible to the final function? Much appreciated, I searched quite a few posts on SO but couldn't find anything directly relevant to my case.

 ntegrand(x, F, K, T1, T2, vol, flag): print(kwargs) d1 = (np.log(x / (x+K)) + 0.5 * (vol**2) * (T2-T1)) / (vol * np.sqrt(T2 - T1)) d2 = d1 - vol*np.sqrt(T2 - T1) mu = np.log(F) - 0.5 *vol**2 * T1 sigma = vol * np.sqrt(T1) value = lognorm.pdf(x, scale=np.exp(mu), s=sigma) * (flag * x*norm.cdf(flag * d1) - flag * (x+K)*norm.cdf(flag * d2)) return value def integrate(x, w, a, **kwargs): return np.sum(w*transform_integral_negative1_1_to_0_1(x, a, **kwargs)) def transform_integral_0_1_to_Infinity(x, a, **kwargs): return integrand(a+(x/(1-x)), **kwargs) *(1/(1-x)**2); def transform_integral_negative1_1_to_0_1(x, a, **kwargs): return 0.5 * transform_integral_0_1_to_Infinity((x+1)/2, a, **kwargs) flag = current_opt[i,0] F = current_opt[i,1] K = current_opt[i,2] T2 = current_opt[i,3] T1 = current_opt[i,4] r = current_opt[i,5] vol = current_opt[i,6] a = 0 b = np.Inf kwargs = {'flag':flag, 'F':F, 'K':K, 'vol':vol, 'T2':T2, 'T1':T1} integrate(x, w, a, **kwargs)

And this is what is printed before the print(kwargs) error is thrown:

 {'F': 1.2075, 'flag': -1.0, 'K': 0.12509999999999999, 'T2': 0.068500000000000005, 'vol': 0.42999999999999999, 'T1': 0.041099999999999998}

And the error, even though K was defined above:

 NameError: name 'K' is not defined

The solution was to define each entry instead of extracting each value from the dictionary... The **kwargs function passed last extracts all keyword arguments automatically.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

**kwargs is passed as a dict to the function, it does not automatically fill the function locales.

e.g:

 kwargs = {'K': 1} def foo(**kwargs): print(K) def bar(**kwargs): print(kwargs['K']) foo(**kwargs) # NameError bar(**kwargs) # Works Ok.
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda