Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
**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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda