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

215
Visualizações
What happens in memory while unpacking a collection?

Say

list1=[4,8,12]

a,b,c=list1

output is a=4,b=8,c=12.

My confusion

Instructor told us that it is not like a gets mapped to 4, b to 8, and c to 12. I didn't understand what he told clearly (although I listened repeatedly to him multiple times). He was telling something like object is created for 4 and a is mapped to 4. But what is the difference between this and what I have presented below in figure?

handwritten diagram

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

0

The thing about your picture that's misleading is that it implies that a, b, and c reference slices of list1. If you change list1, though, you will find that a, b, and c aren't affected by that change.

A better way to draw the picture might be to show 4, 8, and 12 separate from list1:

list1-->[ ][ ][ ]
         |  |  |
         V  V  V
         4  8  12
         ^  ^  ^
         |  |  |
         a  b  c

All of the variables are independent of one another, even though some of them (e.g. list1[0] and a) currently point to the same values.

To put it another way: saying a = list1[0] is saying "evaluate list1[0] and assign a to reference whatever that value is right now", which is not the same as saying "make a be an alias for list1[0]".

over 4 years ago · Santiago Trujillo Relatório

0

Try this:

# define the list
list1=[4,8,12]

# reserve 3 memory spaces and unpack the values from list into them
# those memory spaces will contain one integer each one of the size of
# sys.getsizeof(int()) == 28 bytes (Python 3)
# a,b and c are actually pointers to those memory spaces
a,b,c=list1

print(a,b,c)

# change the first value of the list
list1[0] = 56
print(list1)
    
# now check that indeed "a" is not the same pointer than "list1[0]"
print(a)

But

You must to be careful with this kind of asignations with lists, try also this:

list2 = list1
print(list1, list2)

# then change any of them
list1 [0] = -1

# check that "list2" is pointing to the same memory address than "list1"
print(list1, list2)
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