It’s definitely not the same. Similarly for a class you can define the __add__ dunder method for a + b and separately the __iadd__ dunder method for a += b. The first creates a new object, the latter changes/mutates the existing object a. For immutable types it is the same though.
- 13 Posts
- 24 Comments
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.
Different languages make different choices. The disadvantage of Haskell is that if you want to change one value in a collection of a million values that it either makes a full copy or tries to optimize by sharing values behind the scene, both resulting in significant overhead. Most people already understand that pure functional programming languages don’t deliver except in very specific circumstances: Haskell TIOBE rating 0.32%, https://www.tiobe.com/tiobe-index/
Incorrect sorry, check the “Solution” link for the correct answer.
__add__is called with+and__iadd__is called with+=, and there is a difference: https://www.reddit.com/r/PythonLearning/comments/1nw08wu/right_mental_model_for_python_data/
Yes
ayou get for free to set the tone, the others are more interesting.
The whole point is to practice Python Data Model concepts, it’s not a best-way-to-code example, so feel free to hate.
You are right, in landscape mode it’s better, but still not ideal. It’s a project I don’t have time for now. On the other hand, did you run Python code, in an IDE where the debugger visualizes the whole program state, on your Phone before?
C is incorrect,sorry. See the “Solution” link for the correct answer.
Actually running the code? I got to the stage where only AI can help me understand anything ;-)
Thanks for your feedback, much appriciated.
I agree that an
exercise14.rstwould be nice, but to save time I’ve let the code speak for itself now together with the visualizaion. I’ll probably revisit and better document the exercises later.At the Explanation link I try to give a general explanation about Pyrhon mutability (and copy later on), I agree some readers might find it hard to relate that to a specific exercise, but I don’t want to write a specific explanation for each exercise.
You can just write your code and then press “Get URL” to get the link: https://memory-graph.com/#code=def+fun(a+%3D+[])%3A ++++a+%2B%3D+[1] ++++return+a fun() print(fun())+%23+[1%2C+1]
DuckDuckGo problem, thanks for reporting.
The “Solution” link gives the solution to the exercise, the “Explanation” link explains the Python data model concepts behind the exercise. If some parts are hard to understand let me know.
Yes, that is a surprise to many, in other languages ‘x+=y’ and ‘x=x+y’ are the same.


Copying a list with a million elements every time you make a small change is not fun. Sure, you can optimize a bit behind the scenes, but that still gives a lot of overhead.