Thanks, but I also give a solution link, so people can already cheat if they really want too.
Thanks a lot, I hope it can bring much value for you.
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.
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 |= b and a = 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 += b being equivalent to a = a + b for immutable types, so some people generalize that incorrectly to mutable types too. Otherwise I think it's pretty clear a += b mutates a, and a = a + b first computes a + b and then reassigns that to a so that its identity changes, just like in c = 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 |= b and a = a | b is exactly the same as the difference between a += b and a = 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 += b and a = a + b, try with a tuple.