Skip to content

Lists in classes seem to be synchronized #139972

@TungstenGamut

Description

@TungstenGamut

Bug report

Bug description:

I am not completely sure why this is happening, but here is the minimal reproducible example:

class TestClass:
    someVar: list[str] = []
    def __init__(self, string: str) -> None:

        self.someVar.append(string)

    def __repr__(self):
        return self.someVar.__repr__()

l: list[TestClass] = []
for i in range(10):
    t = TestClass(str(i))
    l.append(t)

print(l)

Expected output:
[['0'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]
Actual output:
[['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']]

My way of fixing has currently been something along the lines of this:

...
def __init__(self) -> None:
    self.someVar = []
...

I have not yet tested this with different types. Also, if I am doing something wrong, please tell me.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions