Skip to content

Mutable default values example - TypeError: 'int' object is not iterable #116535

Closed
@dec1

Description

@dec1

Documentation

The example:

dataclasses.html#mutable-default-values

@dataclass
class D:
    x: list = []      # This code raises ValueError
    def add(self, element):
        self.x += element

not only raises the ValueError being discussed, but also an unwanted TypeError when you call add(), that distracts from the point of the example.
Would be better to call append() to add a single element to the list

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions