[Python] Python doubt in simple class object [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I've been using Python for a very long time. The following has been confusing me today.

    Suppose I have this class called Node:

    class Node:
    def __init__(self, x = []):
    self.x = x


    a = Node()
    b = Node()
    a.x.append(3)
    b.x.append(5)


    To my horror, a.x and b.x both are [3,5]. What is happening here?

    Also, how to I stop this from happening? I just want a.x to be [3] and b.x to be [5], not [3,5].

    Continue reading...

Compartilhe esta Página