-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Docs: clarify object assignments intuition in the tutorial section #116271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's important to note that assignment always behaves the same. Mutability does not change how assignment works. Assignment never copies data, it only makes another name refer to an existing value. |
Why would anyone even think that they do? Honestly that's been puzzling me for quite a while, every time I see people making that mistake. Maybe you have some insights? |
This is a very common mistake. People see the behavior of It might be hard for experts in Python to understand the beginner mindset. Trust me: this is very common. |
I will answer with code: pi = 3.14
tmp = pi
tmp += 1
pi == 3.14 # as expected, remained the same And the same thing with other immutable objects. Python's definition of mutability states that mutable objects can be changed without changing their ID: tmp = {}
tmp_id = id(tmp)
tmp['popular_cheese'] = ""
tmp == {'popular_cheese': 'Illchester'} # expected
tmp_id == id(tmp) # expected as well How should the reference assignment derive from it is still beyond my comprehension :) As well we have another thing that according to Diataxis we shouldn't presume that the reader possesses any special knowledge. |
That's an interesting point. Is there a way to introduce primitive and reference types to the reader? It looks like a mirrored concept with mutability. Do you have counter-examples with mutable primitives or immutable reference types? |
@Privat33r-dev your original suggestion (in IRC) was to show an example using lists to warn people that BTW: Python doesn't have primitive and reference types either (Java does). All values are objects, and assignment treats all values the same. |
I know. I've probably seen it hundreds of times. And I think many times it was people who mentioned they just started programming, so they weren't applying misleading knowledge from another language. I can somewhat follow the reasoning with experience from numbers, and with I think assuming a copy is made is unnatural. That's not happening in the real world. Things don't magically duplicate. If I get a cat and start calling it Bernie, I don't have two cats. If I called someone's dog Alex and then killed Alex, that other person would be angry. Because it's the same dog. And people have lots of experience with the real world. Why does that vast experience not translate? |
I'm not sure what to tell you other than the two numeric examples that we've shown here in this discussion. Do you agree that it would be useful to show that assigning lists doesn't make a copy? I suggested it could go here https://docs.python.org/3/tutorial/introduction.html#lists right before "Assignment to slices is also possible." |
I had to move the |
I guess it would be, yes. But I hope it'll be phrased better than in Google's class, which in my opinion makes it sound like assignment with lists is special. |
…utorial section (#116283) Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
… the Tutorial section (pythonGH-116283) (cherry picked from commit 45a9243) Co-authored-by: Kerim Kabirov <[email protected]> Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
… the Tutorial section (pythonGH-116283) (cherry picked from commit 45a9243) Co-authored-by: Kerim Kabirov <[email protected]> Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
…n the Tutorial section (GH-116283) (#116306) Co-authored-by: Kerim Kabirov <[email protected]> Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
…n the Tutorial section (GH-116283) (#116305) Co-authored-by: Kerim Kabirov <[email protected]> Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
Thank you for the issue and PR! |
… the Tutorial section (python#116283) Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
… the Tutorial section (python#116283) Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
… the Tutorial section (python#116283) Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
I have another interesting answer for your question. COW. As in the C++ example. |
Uh oh!
There was an error while loading. Please reload this page.
Documentation
In the Tutorial section of the Python Documentation, 2 parts (introduction and datastructures) are explaining
list
s and none of them go deeper in the concept of mutability in Python: lists (or other mutable) assignments are just essentially a pointer assignments and they do not create a new object, but rather create another reference to the same object, which might lead to unexpected results.Mentions
@nedbat gave an advice regarding better positioning of the in the IRC channel
Sidenote
While researching this topic, I found a Google's paper on the topic that starts introduction to the
list
concept with this peculiarity.Linked PRs
The text was updated successfully, but these errors were encountered: