The point of the visualization at the Solution link is precisely to help people get the right model of how labels and the data model in general works in Python. See the Explanation link for more details.
- 19 Posts
- 31 Comments
Great, I hope it can bring much value for your teaching.
If you are interested see the dictionary docs (https://docs.python.org/3/builtins/stdtypes.html#dict):
d |= otherUpdate the dictionarydwith keys and values fromother, which may be either a mapping or an iterable of key/value pairs. The values ofothertake priority whendandothershare keys. Added in version 3.9.Pick your answer and check the Solution link.
There is one important difference between
a |= banda = a | b, that is one part that makes this exercise difficult. See the dictionary docs (https://docs.python.org/3/builtins/stdtypes.html#dict):d |= otherUpdate the dictionarydwith keys and values fromother, which may be either a mapping or an iterable of key/value pairs. The values ofothertake priority whendandothershare keys. Added in version 3.9.Pick your answer and check the Solution link.
The zen of Python lies unless you’re Dutch. I’m Dutch and when I look out of my office I see the CWI where Guido wrote the original Python versions back in the day, see “Python the documentory” on YouTube.
I’ve built a nice visualizer for internalizing what is going on under the hood, I hope that can help people.
I think the confusion comes from
a += bbeing equivalent toa = a + bfor immutable types, so some people generalize that incorrectly to mutable types too. Otherwise I think it’s pretty cleara += bmutatesa, anda = a + bfirst computesa + band then reassigns that toaso that its identity changes, just like inc = a + b.If you implement these operations in a class you have to implement each dunder,
__iadd__(self, other)and__add__(self, other)separately, same thing in C++.
Thanks, maybe I should have added that possible answer instead of ‘D’, adding one more feels too many.
If you don’t want mutability you have to go to a pure functional language like Haskell, but then you have to copy a big list every time you make a change. There are ways to optimize copying by secretly sharing data behind the scene but you pay a performance price in some way. Then again, Python is slow and has mutability but popular for other reasons.
Thanks, I’m pretty happy with it myself.
The difference between
a |= banda = a | bis exactly the same as the difference betweena += banda = a + b. Nothing new about that, try:a = [1] b = a b += [2] b.append(3) b = b + [4] b.append(5) print(a)or see: https://fosstodon.org/@bterwijn/116328991093782469
For immutable types there is no difference between
a += banda = a + b, try with a tuple.
Diagnostics came up with some “double percent-encoding” issue on IOS. I’ve updated the website with a workaround, hopefully it does work now. Thanks for bug reporting.
This is what the solution link is supposed to give you at the end of the execution animation:

It’s hard to support all mobile browsers, on some it does work.
It’s mainly targeted to Python programmers at: https://programming.dev/c/python
The Solution link gives you a visualization of program execution, I’ve started to make the same sort of visualizer for C. What do you think?
Can you tell me what the problem is for you when you click the first link? I tried to make it work for all browsers (even mobile, but the UI isn’t very mobile friendly), so I would like to get it working on your system too. If you can give me a bug report (and maybe try from different system), that would be great.
The second link explains:
- mutability: https://github.com/bterwijn/memory_graph#mutable-type
- shallow copy: https://github.com/bterwijn/memory_graph#copying-values-of-mutable-type
- difference between
x += yandx = x + y: https://github.com/bterwijn/memory_graph#name-rebinding
It’s left to the reader to generalize this to
x |= yvsx = x | y.
bterwijn@programming.devOPto
Python@programming.dev•Difference between 'instance', 'class', and 'static' method visually explained
0·4 months agoAnother common naming convention in C++ is using an underscore as prefix for member variables: int _memberVariable{123};
bterwijn@programming.devto
Python@programming.dev•List comprehensions vs generator expressions
0·4 months agoI like to use this example with my students: https://github.com/bterwijn/memory_graph#lazy-evaluation
bterwijn@programming.devOPto
Python@programming.dev•Python's Data Model Explained through Visualization
0·7 months agoIt’s definitely not the same. Similarly for a class you can define the
__add__dunder method fora + band separately the__iadd__dunder method fora += b. The first creates a new object, the latter changes/mutates the existing objecta. For immutable types it is the same though.
Nice one, see the “Solution” link for correct answer.
Yes I understand your point, but I’m trying to reach out so people are aware and can use it in Python education. I feel it can really help beginners understand tricky concepts with ease, bit it’s hard to reach a bigger audience these days. Sorry for the repetition, I’ll guess I should cut back a bit.
Thanks, but I also give a solution link, so people can already cheat if they really want too.