[Python] Does Python make a copy of objects on assignment?

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    When I try this code:

    dict_a = dict_b = dict_c = {}
    dict_c['hello'] = 'goodbye'

    print(dict_a)
    print(dict_b)
    print(dict_c)


    I expected that it would just initialise the dict_a, dict_b and dict_c dictionaries, and then assign a key in dict_c, resulting in

    {}
    {}
    {'hello': 'goodbye'}


    But it seems to have a copy-through effect instead:

    {'hello': 'goodbye'}
    {'hello': 'goodbye'}
    {'hello': 'goodbye'}


    Why?


    See also:


    Continue reading...

Compartilhe esta Página