def fun(a = []):
a += [1]
return a
fun()
print(fun()) # [1, 1]
::: spoiler issues I had with the tool you linked
I tried to make a link to this code in this visual debugger, but a link is made broken and can't be used without urlencoding spaces (I guess). Also the tool doesn't produce visual output in DuckDuckGo browser on Android
:::
The following URL has been copied to your clipboard with:
The current code
Set breakpoints
Playback timestep
Play/Pause state
https://memory-graph.com/#code=+%23+What+is+the+output+of+this+Python+program? +++ def+fun(a+=+[]):+ ++++a++=+[1] ++++return+a fun()+print(fun())
The latter doesn't work, and I didn't think of checking the former
Structlog actually bit me like this! Had a dict that I manipulated:
::: spoiler expectation spoilers
expected a copy, but actually modified the original dict. Just did a my_dict.copy() (IIRC - I don't remember whether I needed a deep copy or not)
:::
edit: goeie shit, Bas! Leer je de studenten wel debuggen en unit testing? Dat was echt gemis toen ik klaar was bij de HU, en vooral unit testen zou veel hebben geholpen tijdens het maken van mijn code.
To be fair, I had to read the linked question on SO to understand what was the possible alternative. I do expect that if I use += it is in place.
On the other hand with Python and Java I always keep forgetting if everything is by value or by reference, I really miss some extra clarity of the languages where you can see if something is mutable when passed to a function
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.
I agree that an exercise14.rst would 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.
Ah! Haven't yet reached stubborn old geezer stage.
Don't immediately understand exactly what the code does? Must be cuz of mental slippage. Like brain plague or residual PHP or jinja2 knowhow that can't be purged.
We all know it's not from drug use or excessive party lifestyle. Maybe excessive retro Linux gaming?
Whatever it is, those exercises are great diagnosis tool.
20 Comments
sukhmel@programming.dev · 6 pts · 350d
There's also a gotcha with default parameters:
::: spoiler issues I had with the tool you linked I tried to make a link to this code in this visual debugger, but a link is made broken and can't be used without urlencoding spaces (I guess). Also the tool doesn't produce visual output in DuckDuckGo browser on Android :::
bterwijn@programming.dev · 2 pts · 350d
You can just write your code and then press "Get URL" to get the link: https://memory-graph.com/#code=def+fun%28a+%3D+%5B%5D%29%3A%0A++++a+%2B%3D+%5B1%5D%0A++++return+a%0A%0Afun%28%29%0Aprint%28fun%28%29%29+%23+%5B1%2C+1%5D
DuckDuckGo problem, thanks for reporting.
sukhmel@programming.dev · 2 pts · 350d
Yeah, I wrote code and pressed get URL, but then I copied what was shown in the window
The clipboard content:
The window text:
The latter doesn't work, and I didn't think of checking the former
bterwijn@programming.dev · 2 pts · 350d
Thanks for reporting, should be fixed now.
lascapi@jlai.lu · 5 pts · 350d
Wow, nice project! I'm a 'visual person', I understand better when I see what's happening.
That can help me a lot! :)
bterwijn@programming.dev · 3 pts · 350d
Thanks, glad it helps you.
NostraDavid@programming.dev · 2 pts · 350d
Structlogactually bit me like this! Had a dict that I manipulated: ::: spoiler expectation spoilers expected a copy, but actually modified the original dict. Just did amy_dict.copy()(IIRC - I don't remember whether I needed a deep copy or not) :::edit: goeie shit, Bas! Leer je de studenten wel debuggen en unit testing? Dat was echt gemis toen ik klaar was bij de HU, en vooral unit testen zou veel hebben geholpen tijdens het maken van mijn code.
wewbull@feddit.uk · 2 pts · 350d
As I would expect. Does the result surprise people?
If so, rename the arguments to
fun()to be r, s, t, and u. Just inside the function. Does the obvious two scopes now make it clearer?shape_warrior_t@programming.dev · 7 pts · 350d
The fact that
x += ymodifieslists in place might be surprising if you're expecting it to be exactly equivalent tox = x + y.bterwijn@programming.dev · 3 pts · 350d
Yes, that is a surprise to many, in other languages 'x+=y' and 'x=x+y' are the same.
sukhmel@programming.dev · 2 pts · 350d
To be fair, I had to read the linked question on SO to understand what was the possible alternative. I do expect that if I use
+=it is in place.On the other hand with Python and Java I always keep forgetting if everything is by value or by reference, I really miss some extra clarity of the languages where you can see if something is mutable when passed to a function
5C5C5C@programming.dev · 3 pts · 350d
Looks like this whole post is an ad for someone's project. The links hardly have to do with the post.
bterwijn@programming.dev · 4 pts · 350d
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.
logging_strict@programming.dev · 0 pts · 350d
Yeah and the link went to a site with in-browser remote code execution. Dodgy.
Clicking Play or GetURL did nothing.
Luckily my browser is as old as me. And was too grumpy to actually do what it's told.
logging_strict@programming.dev · 1 pts · 350d
Here is the link to the code which is being in-browser remote executed
exercise14.py
There is no "explanation" next to or within the file. A
exercise14.rstortest_exercise14.pywould be nice.The Explanation link provided contains a mountain of visual shit (aka noise) none of which looks like
exercise14.pycode.The OP is trying to teach us, i get that. Looks like i'm failing the (mountain of visual shit) reading comprehension section of this exercise.
bterwijn@programming.dev · 2 pts · 349d
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.
logging_strict@programming.dev · 1 pts · 349d
First i love the visualizations. It grew on me. And supportive of coding challenges posts. As long as there isn't a flood of them. Which there isn't.
I get your position. That there just isn't an explanation for each and every exercise. The other comments made it seemed like there was.
When i clicked on the Explanation link, the browser didn't initially scroll to the Mutability section. Went downhill from there.
btw i'm too dumb to know the answer and too lazy to copy+paste the code into a REPR.
All the other commenters are just dishonest pretending they totally got it without running the code ;-)
bterwijn@programming.dev · 2 pts · 348d
Actually running the code? I got to the stage where only AI can help me understand anything ;-)
logging_strict@programming.dev · 1 pts · 348d
AI reads github issues and crapoverflow for us cuz that is where packages are mainly documented.
The package documentation is a historical document according to every PEP ever. ;-)
What if github issues&discussions disappeared tomorrow? That feeling in your ballsack is what free falling feels like?
Forever wars? np we are due for some excitement.
github disappearing suddenly ... that keeps me awake at night
Is Copilot unemployment in it's future?
logging_strict@programming.dev · 1 pts · 348d
Ah! Haven't yet reached stubborn old geezer stage.
Don't immediately understand exactly what the code does? Must be cuz of mental slippage. Like brain plague or residual PHP or jinja2 knowhow that can't be purged.
We all know it's not from drug use or excessive party lifestyle. Maybe excessive retro Linux gaming?
Whatever it is, those exercises are great diagnosis tool.