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

437
Visualizações
Why use **kwargs in python? What are some real world advantages over using named arguments?

I come from a training in static languages. Can someone explain (ideally through an example) the real-world advantages of using **kwargs over named arguments ?

To me it just seems to make the function call more ambiguous. Thank you.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You may want to accept arguments with almost arbitrary names for a number of reasons, and that's what **kw lets you do with the form.

The most common reason is to pass the arguments directly to some other function that is wrapping (decorators are one case of this, but FAR from the only one!) -- in this case, **kw loosens the coupling between wrapper and wrapper , as the wrapper does not have to know or care about all of the wrapper's arguments. Here's another completely different reason:

 d = dict(a=1, b=2, c=3, d=4)

if all the names had to be known in advance, then obviously this approach just couldn't exist, right? And by the way, where appropriate, I prefer this way of doing a dict whose keys are literal strings for:

 d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

simply because the latter has quite a bit of punctuation and is therefore less readable.

When none of the excellent reasons for accepting **kwargs apply, then don't accept it: it's that simple. IOW, if there's no good reason to allow the caller to pass additional arguments with arbitrary names, don't let that happen, just avoid putting a **kw form at the end of the function signature in the def statement.

As for using **kw in a call, that allows you to gather the exact set of named arguments you need to pass, each with corresponding values, into a dict, regardless of a single call point, then use that dict in the only point of call. Compare:

 if x: kw['x'] = x if y: kw['y'] = y f(**kw)

to:

 if x: if y: f(x=x, y=y) else: f(x=x) else: if y: f(y=y) else: f()

Even with only two possibilities (and of the simplest kind!), the lack of **kw already makes the second option absolutely untenable and intolerable; imagine how it plays out when there are half a dozen possibilities, possibly in a slightly richer interaction. without **kw , life would be absolute hell under such circumstances!

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