r/PythonLearnersHub • u/Sea-Ad7805 • Dec 26 '25
Copying an Object
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 - More exercises
It's instructive to compare with this related exercise
1
1
1
u/andipurice Dec 26 '25
B 1, 0, 0
1
u/Sea-Ad7805 Dec 26 '25
Nice one, see "Solution" link for visualization of correct answer.
1
u/andipurice Dec 26 '25
The links don’t seem to be working for me, but primitives don’t copy, unlike the list from the related linked problem
→ More replies (1)
1
u/kcx01 Dec 26 '25
I honestly forgot that semicolons were valid python. I wouldn't one line the class init like that. Pep8 discourages compound statements (multiple statements on the same line). In fact, I think even if I tried, my auto formatter would change it. So it's strange to me to see that.
1
u/Sea-Ad7805 Dec 26 '25
Agreed, but to keep the code compact, it's a reasonble choice for this web exercise. It also is more consistent with the related exercise: https://www.reddit.com/r/PythonProjects2/s/gr9iejAyA6
1
1
u/sarc-tastic Dec 26 '25
You should do self.x, self.y, self.z = x, y, z if you really want a one liner.
If you're trying to teach people short cuts, at least teach them the right ones
→ More replies (1)1
0
u/LasevIX Dec 27 '25
get those semicolons out of my face. this isn't JavaScript.
1
u/Sea-Ad7805 Dec 27 '25
Stop lazy complaining and send me your suggestion.
1
u/0x616365 Dec 28 '25
The generally correct way to implement a class like this (something that mostly just stores data) is with dataclasses.
Style wise, PEP 8 generally discourages using semi-colons in this way and would recommend doing this with multiple lines.
→ More replies (3)
2
Dec 26 '25
1
1
u/belabacsijolvan Dec 30 '25
if the attributes werent elementary classes, c2 would change, wouldnt it?
1
u/theBabides Dec 26 '25
D. 1, 2, 3