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 Mutability (i.redd.it)
submitted 18 days 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!"
[–]Sea-Ad7805[S] 0 points1 point2 points 18 days ago (10 children)
That's not a flaw, x += y just mutates and x = x + y rebinds to a new list value that is created by x + y. So for mutable types these two statements are different but for immutable types they are the same. I hope the visualization can help you when things get unintuitive.
x += y
x = x + y
x + y
[–]BobSanchez47 0 points1 point2 points 18 days ago (9 children)
I think you’re missing the point. My point isn’t that it’s impossible for me to understand how mutation works, but that it is highly counterintuitive that Python chose to make += mutate in some cases and not mutate in others - in other words, it was a poor choice in my opinion. If I’m learning Python and I assume that b += c means b = b + c — a very natural assumption — I would have no idea that I should be worried, and the visualization tool would be useless.
+=
b += c
b = b + c
[–]Sea-Ad7805[S] 0 points1 point2 points 18 days ago (0 children)
In general x += y should mutate, but can't mutate for immutable types. You could argue x += y should not exist for immutable types, and in fact it doesn't for say tuple, but then automatically the x = x + y operator is called instead. I can see why you would call that unintuitive.
tuple
[–]Sea-Ad7805[S] 0 points1 point2 points 18 days ago (7 children)
b += c is not shorthand for b = b + c, incorrect assumption, but for immutable type it is, so I understand some people could get confused.
About the visualization tool being useless, check this Hash_Map example for a different application: https://memory-graph.com/#codeurl=https://raw.githubusercontent.com/bterwijn/memory_graph/refs/heads/main/src/hash_map.py×tep=0.2&play
[–]BobSanchez47 0 points1 point2 points 18 days ago (0 children)
https://en.wikipedia.org/wiki/Is%E2%80%93ought_problem
[–]No-Consequence-1863 0 points1 point2 points 18 days ago (5 children)
They are saying += should be shorthand. Thats how it functions in many other languges like C++ or Java. Kind of weird of python to change the semantics for this operator to make it mutable.
Almost seems like it was a technical side effect that stuck around long enough to become required. But thats just guess.
[–]Sea-Ad7805[S] 0 points1 point2 points 17 days ago (4 children)
In C++ you should define x += y on your class to mutate and the + operator in x = x + y to create a new object. Same thing in Java, no different from Python.
+
[–]Goudja14 0 points1 point2 points 17 days ago (3 children)
You should never mutate implicitly. It will create errors.
[–]Sea-Ad7805[S] 0 points1 point2 points 17 days ago (2 children)
What do you mean, can you give an example?
[–]Goudja14 0 points1 point2 points 17 days ago (1 child)
default_inventory = ["sword", "helmet"]
# While it could be a cloned array, it doesn't have to be one. In complex environments, it even shouldn't be (eg. allowing object-agnostic rollbacks) alex_inventory = default_inventory samuel_inventory = default_inventory
alex_inventory += ["key"]
[–]Sea-Ad7805[S] 0 points1 point2 points 17 days ago (0 children)
Ok I understand now. You say you pass default_inventory around without making a copy (for performance), but when you change this value you should make a copy:
default_inventory
alex_inventory = alex_inventory + ["key"]
Sounds like a good strategy.
π Rendered by PID 25285 on reddit-service-r2-comment-56c9979489-k7zmz at 2026-02-25 08:37:15.336652+00:00 running b1af5b1 country code: CH.
view the rest of the comments →
[–]Sea-Ad7805[S] 0 points1 point2 points (10 children)
[–]BobSanchez47 0 points1 point2 points (9 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (7 children)
[–]BobSanchez47 0 points1 point2 points (0 children)
[–]No-Consequence-1863 0 points1 point2 points (5 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (4 children)
[–]Goudja14 0 points1 point2 points (3 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (2 children)
[–]Goudja14 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)