Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dec1 opened this issue Mar 9, 2024 · 2 comments
Closed

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

dec1 opened this issue Mar 9, 2024 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@dec1
Copy link
Contributor

dec1 commented Mar 9, 2024

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

@Privat33r-dev
Copy link
Contributor

Privat33r-dev commented Mar 9, 2024

Your point is valid. And what's more, using .append() instead of += would match the style used in the first example (without dataclasses), which is going to increase consistency and remove distractions.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 9, 2024
…h-116538)

(cherry picked from commit db8f423)

Co-authored-by: Declan <5962877+dec1@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 9, 2024
…h-116538)

(cherry picked from commit db8f423)

Co-authored-by: Declan <5962877+dec1@users.noreply.github.com>
ericvsmith pushed a commit that referenced this issue Mar 9, 2024
) (gh-116551)

gh-116535: Fix distracting "TypeError" in example code (gh-116538)
(cherry picked from commit db8f423)

Co-authored-by: Declan <5962877+dec1@users.noreply.github.com>
ericvsmith pushed a commit that referenced this issue Mar 9, 2024
) (gh-116552)

gh-116535: Fix distracting "TypeError" in example code (gh-116538)
(cherry picked from commit db8f423)

Co-authored-by: Declan <5962877+dec1@users.noreply.github.com>
@ericvsmith
Copy link
Member

Merged. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

3 participants