▲ 19 ▼ Python Mutability (programming.dev) submitted 1 year ago by bterwijn@programming.dev to c/python@programming.dev 6 comments fedilink hide all child comments An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening: Solution Explanation
[–] joyjoy@lemmy.zip 3 points 1 year ago (3 children) It's always fun to guess whether python will call __add__ or __iadd__. permalink fedilink source hideshow 3 child comments replies: [–] bterwijn@programming.dev [S] 3 points 1 year ago (2 children) __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/ permalink fedilink source parent hideshow 2 child comments replies: [–] joyjoy@lemmy.zip 3 points 1 year ago* (1 child) Except if __iadd__ doesn't exist on the type, then __add__ is called. The variable is always reassigned. Quick example: x = (0, [1, 2]) try: x[1] += [3] # calls list.__iadd__, then reassigns x[1] except TypeError as e: print(e) x += (4,) # calls tuple.__add__, then reassigns x print(x) What is printed? permalink fedilink source parent hideshow 1 child comment replies: [–] bterwijn@programming.dev [S] 1 point 1 year ago Thanks. I didn't know: https://memory-graph.com/#code=x+%3D+%280%2C+%5B1%2C+2%5D%29%0Atry%3A%0A++++x%5B1%5D+%2B%3D+%5B3%5D++%23+calls+list.__iadd__%2C+then+reassigns+x%5B1%5D%0Aexcept+TypeError+as+e%3A%0A++++print%28e%29%0A%0Ax+%2B%3D+%284%2C%29++%23+calls+tuple.__add__%2C+then+reassigns+x%0A%0Aprint%28x%29%0A%0A%0Aclass+MyClass%3A%0A++++%0A++++def+__add__%28self%2C+other%29%3A%0A++++++++print%28%27__add__%27%29%0A++++++++%0A++++%23def+__iadd__%28self%2C+other%29%3A%0A++++%23++++print%28%27__iadd__%27%29%0A%0Aa+%3D+MyClass%28%29%0Ab+%3D+MyClass%28%29%0Aa+%2B%3D+b++%23+calls+__add__+if+__iadd_+doesn%27t+exist%0A&play= permalink fedilink source parent
[–] bterwijn@programming.dev [S] 3 points 1 year ago (2 children) __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/ permalink fedilink source parent hideshow 2 child comments replies: [–] joyjoy@lemmy.zip 3 points 1 year ago* (1 child) Except if __iadd__ doesn't exist on the type, then __add__ is called. The variable is always reassigned. Quick example: x = (0, [1, 2]) try: x[1] += [3] # calls list.__iadd__, then reassigns x[1] except TypeError as e: print(e) x += (4,) # calls tuple.__add__, then reassigns x print(x) What is printed? permalink fedilink source parent hideshow 1 child comment replies: [–] bterwijn@programming.dev [S] 1 point 1 year ago Thanks. I didn't know: https://memory-graph.com/#code=x+%3D+%280%2C+%5B1%2C+2%5D%29%0Atry%3A%0A++++x%5B1%5D+%2B%3D+%5B3%5D++%23+calls+list.__iadd__%2C+then+reassigns+x%5B1%5D%0Aexcept+TypeError+as+e%3A%0A++++print%28e%29%0A%0Ax+%2B%3D+%284%2C%29++%23+calls+tuple.__add__%2C+then+reassigns+x%0A%0Aprint%28x%29%0A%0A%0Aclass+MyClass%3A%0A++++%0A++++def+__add__%28self%2C+other%29%3A%0A++++++++print%28%27__add__%27%29%0A++++++++%0A++++%23def+__iadd__%28self%2C+other%29%3A%0A++++%23++++print%28%27__iadd__%27%29%0A%0Aa+%3D+MyClass%28%29%0Ab+%3D+MyClass%28%29%0Aa+%2B%3D+b++%23+calls+__add__+if+__iadd_+doesn%27t+exist%0A&play= permalink fedilink source parent
[–] joyjoy@lemmy.zip 3 points 1 year ago* (1 child) Except if __iadd__ doesn't exist on the type, then __add__ is called. The variable is always reassigned. Quick example: x = (0, [1, 2]) try: x[1] += [3] # calls list.__iadd__, then reassigns x[1] except TypeError as e: print(e) x += (4,) # calls tuple.__add__, then reassigns x print(x) What is printed? permalink fedilink source parent hideshow 1 child comment replies: [–] bterwijn@programming.dev [S] 1 point 1 year ago Thanks. I didn't know: https://memory-graph.com/#code=x+%3D+%280%2C+%5B1%2C+2%5D%29%0Atry%3A%0A++++x%5B1%5D+%2B%3D+%5B3%5D++%23+calls+list.__iadd__%2C+then+reassigns+x%5B1%5D%0Aexcept+TypeError+as+e%3A%0A++++print%28e%29%0A%0Ax+%2B%3D+%284%2C%29++%23+calls+tuple.__add__%2C+then+reassigns+x%0A%0Aprint%28x%29%0A%0A%0Aclass+MyClass%3A%0A++++%0A++++def+__add__%28self%2C+other%29%3A%0A++++++++print%28%27__add__%27%29%0A++++++++%0A++++%23def+__iadd__%28self%2C+other%29%3A%0A++++%23++++print%28%27__iadd__%27%29%0A%0Aa+%3D+MyClass%28%29%0Ab+%3D+MyClass%28%29%0Aa+%2B%3D+b++%23+calls+__add__+if+__iadd_+doesn%27t+exist%0A&play= permalink fedilink source parent
[–] bterwijn@programming.dev [S] 1 point 1 year ago Thanks. I didn't know: https://memory-graph.com/#code=x+%3D+%280%2C+%5B1%2C+2%5D%29%0Atry%3A%0A++++x%5B1%5D+%2B%3D+%5B3%5D++%23+calls+list.__iadd__%2C+then+reassigns+x%5B1%5D%0Aexcept+TypeError+as+e%3A%0A++++print%28e%29%0A%0Ax+%2B%3D+%284%2C%29++%23+calls+tuple.__add__%2C+then+reassigns+x%0A%0Aprint%28x%29%0A%0A%0Aclass+MyClass%3A%0A++++%0A++++def+__add__%28self%2C+other%29%3A%0A++++++++print%28%27__add__%27%29%0A++++++++%0A++++%23def+__iadd__%28self%2C+other%29%3A%0A++++%23++++print%28%27__iadd__%27%29%0A%0Aa+%3D+MyClass%28%29%0Ab+%3D+MyClass%28%29%0Aa+%2B%3D+b++%23+calls+__add__+if+__iadd_+doesn%27t+exist%0A&play= permalink fedilink source parent
[–] Valmond@lemmy.world 1 point 1 year ago (1 child) I so wonder what a will result in. permalink fedilink source hideshow 1 child comment replies: [–] bterwijn@programming.dev [S] 1 point 1 year ago Yes a you get for free to set the tone, the others are more interesting. permalink fedilink source parent
[–] bterwijn@programming.dev [S] 1 point 1 year ago Yes a you get for free to set the tone, the others are more interesting. permalink fedilink source parent
all 6 comments