[Python] Why does Online Python Tutor present this immutable integer as two different integers...

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    In Fluent Python, By Luciano Ramalho, Chapter 8, Copies Are Shallow by Default, there is an example:

    >>> listOne = [3, [55, 44], (7, 8, 9)]
    >>> listTwo = list(listOne)
    >>> listTwo
    [3, [55, 44], (7, 8, 9)]
    >>> listTwo == listOne
    True
    >>> listTwo is listOne
    False


    The author suggests that we should run through this code using Online Python Tutor to see what is happening step by step.

    I executed the first two lines using Online Python Tutor, and this is the screen shot I got:

    [​IMG]

    What confuses me is:

    All three elements from the each list, the immutable integer, the list and the tuple are actually the same, e.g.

    listOne[0] is listTwo[0] #True
    listOne[1] is listTwo[1] #True
    listOne[2] is listTwo[2] #True


    So why the graph shows two separate 3s at the beginning of their respective list?

    Continue reading...

Compartilhe esta Página