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

208
Visualizações
Why does starred assignment produce lists and not tuples?

In python, I can write something like this:

some_list = [(1, 2, 3), (3, 2, 1)]

for i, *args in some_list:
   print(args)

I will get the next output:

[2, 3]
[2, 1]

When we use *args as function arguments, it is unpacked into a tuple.

Why do we receive a list in this situation?

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

0

It is just a design decision. Making it a tuple was debated in the PEP 3132, but rejected on usability grounds:

Make the starred target a tuple instead of a list. This would be consistent with a function's *args, but make further processing of the result harder.

Simlarly, making it of the same type as the iterable on the rhs of the assignment, was rejected:

Try to give the starred target the same type as the source iterable, for example, b in a, *b = 'hello' would be assigned the string 'ello'. This may seem nice, but is impossible to get right consistently with all iterables.

The very example of yours is listed in the same PEP under specification.

Some reasoning is found in the mailing list of that debate.

When dealing with an iterator, you don't know the length in advance, so the only way to get a tuple would be to produce a list first and then create a tuple from it.

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