« first day (395 days earlier)      last day (440 days later) » 

01:14
probably an XY problem
 
19 hours later…
20:17
Is there a canonical that would cover this question? Why variable assignment behaves differently in Python?
There is an FAQ entry: Why did changing list ‘y’ also change list ‘x’? and I tried searching for its title on SO but didn't find an exact match.
The fact they used ints and lists in two examples muddies the waters
It has to be this but I see you've trodden that path before
yeah, that only talks about lists and doesn't contrast with immutable types / assignment
I'm not aware of a dupe that's gonna cover both bases at once. You've given more than enough material to the OP so maybe it should be closed for being too broad. It's a simple question but it covers two distinct cases
It's not going to be a canonical itself. Nobody loses if the question is closed before some garbage answer
I'm on my phone but it's "fun" to see all the deleted answers already
20:35
I'm pretty sure I've seen a question that contrasts rebinding and mutating, I just can't find it now (though now that I think about it, I wasn't searching for those terms). But I'd be fine with closing it under two or more questions if that's the best we can do. This one's pretty darn close for the rebinding aspect but the title sucks: python byRef // copy
<performs the @KarlKnechtel summoning dance>
My opinion would be to dupe hammer and find a better name for the target question
20:51
The way I see it, if a question as common as this one is this difficult to find a duplicate for, then the existing content simply isn't good enough. So I would think of it as an opportunity: There's a new question with the potential to blow up and become HNQ. With some luck and one or two good answers, it might just become the thing we need.
Now if only we weren't all too lazy to post a good answer (:
Juanpa did it for us :]
Sigh
so, then, people actually get confused by the example that I'm used to using to fix their intuition about function-calling cases, class-attribute-modification cases, etc.
I think the best general presentation of the problem that I have in my list is stackoverflow.com/questions/575196 . But this is a case where an artificial canonical might be superior, because none of the questions is exactly right.
@wjandrea Please also leave some mention of this in the python-canon-discussion room so there's a log of it that curation-centric users can more easily find)
(as an aside, I think a lot of juanpa's earlier, more popular answers could do with improvement, especially given changing site standards over time. Those ones, and of course my own, are what I keep noticing)
The linked and related questions of stackoverflow.com/questions/11690220/python-byref-copy also seem ripe for investigation.
I would have been here faster, but I was busy writing something on Meta
21:10
Karl asked me to post here:
Is there a canonical that would cover this question? [Why variable assignment behaves differently in Python?](https://stackoverflow.com/questions/77153141/why-variable-assignment-behaves-differently-in-python)
There is an FAQ entry: [Why did changing list ‘y’ also change list ‘x’?](https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x) and I tried searching for its title on SO but didn't find an exact match.
I commented two more close ones: [ [Why can a function modify some arguments as perceived by the caller, but not others?]
[not sure how the formatting got messed up there]
 
2 hours later…
23:08
@wjandrea and roganjosh: The OP is coing from Java, where the assignment m = n behaves differently in Java depending on whether m, n are int(primitives in Java) or boxed types (Integer); due to Java automatically providing auto-boxing and unboxing. So the OP's confusion here is not just about whether there is aliasing in the list assignment visitor = names...
... So ask the OP to clarify what (Java-like) behavior they expect on the m = n assignment; don't just try to close as being a dupe of aliasing on the list assignment.
The OP's first sentence "I am new to Python and I am quite confused about the following code" was a strong hint that they were coming from another language and were asking why the behavior in that language is different... should have asked which language and to specify which expected behavior (instead of "I am confused"). Not just cite Batchelder's essay or canonicals on list aliasing, which will not explain anything to Java programmers about why Python doesn't do boxed Integer assignments.
@wjandrea We need a "What are the main ways Python differs from Java?"* or the more applied and prescriptive "What are the pitfalls in Python for programmers coming from Java?, but those are bigger than a single canonical, so maybe a set of canonicals: "What are the pitfalls in Python ASSIGNMENTS for programmers coming from Java?, "... LISTS/ARRAYS...", "... FUNCTIONS...". I can't see many people volunteering to write those, but we'll critique your draft if you do...
23:39
@smci How do you know they're coming from Java? I checked their post history but everything's R, which I don't know myself so I don't know how assignment works.

« first day (395 days earlier)      last day (440 days later) »