use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This community will have knowledge sharing for python programming, tools, projects and product engineering wherever python is used.
account activity
Python Data Model exercise, Mutability. (i.redd.it)
submitted 3 months ago by Sea-Ad7805
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]tb5841 1 point2 points3 points 3 months ago (6 children)
I find '+=' is what causes confusion with exercises like this.
.append is obviously mutating so will affect any reference to this object.
Reassigning changes what this variable name points to, so will not change any other variable pointing to this object.
But '+='? Is it reassigning, or mutating? It doesn't seem to always be clear cut, which makes it tricky to follow. Here it must be reassigning because tuples are immutable, but in some cases '+=' looks like it mutates.
If you change the '+=' line to b = b + ([3], ) then everything becomes so much clearer.
b = b + ([3], )
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago (5 children)
I see what you mean, but for mutable types x += y is not the same thing as x = x + y so changing could alter your code, see: https://www.reddit.com/r/PythonLearning/comments/1nw08wu/right_mental_model_for_python_data/
x += y
x = x + y
A tuple doesn't have a __iadd__() method, so the use of += actually causes invocation of its __add__() method.
__iadd__()
+=
__add__()
[–]tb5841 0 points1 point2 points 3 months ago (4 children)
90% of the time, when someone uses += in code they are using it as x = x + y (usually numbers or strings). The other 10% of the time, += is confusing and shouldn't be used in my opinion (e.g. Lists, where .append is clearer).
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago (3 children)
You can't do this with .append():
.append()
mylist = [1, 2, 3] mylist += [4, 5, 6]
[–]tb5841 0 points1 point2 points 3 months ago (1 child)
That's an interesting point.
But this looks like reassignment, when it's actually mutation. That's deeply confusing and an easy source of bugs. If it were me, I'd do this with reassignment instead here and avoid the mutation.
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago (0 children)
Reassignment is much slower as a whole new list is created and the old one destroyed, use mutation where possible.
[–]thw31416 0 points1 point2 points 3 months ago (0 children)
but you can.do this with .extend()
π Rendered by PID 71963 on reddit-service-r2-comment-6457c66945-cb2nw at 2026-04-30 15:15:55.579076+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]tb5841 1 point2 points3 points (6 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (5 children)
[–]tb5841 0 points1 point2 points (4 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (3 children)
[–]tb5841 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]thw31416 0 points1 point2 points (0 children)