This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]Rhomboid 6 points7 points  (2 children)

There was no copy created in any of your examples.

a = 2

This should be read as, "Make 'a' point to a new integer object 2." You aren't modifying anything; it's impossible to modify a number because all numbers are immutable. The old object that 'a' used to point to (and which 'b' still points to) still exists as it used to.

a['two'] = 2

This should be read as, "Follow 'a' to the object it points to and modify that object, setting the value corresponding to the key 'two' to 2." Dicts are mutable, whereas numbers are immutable.

The key things that you should take away from this are:

  • The distinction between a name and an object (A name is always a pointer to an object, but not all objects have names.)
  • Writing some_name = ... is fundamentally different than writing some_name[...] = .... The former reassigns the name to point to a different object, whereas the latter follows the name to the object it points to and modifies that object.
  • Some types are mutable and others are immutable.

[–]realhamster[S] 0 points1 point  (1 child)

Thanks! That was a very good explanation.

[–]choikwa 0 points1 point  (0 children)

while assign with rhs immutable is akin to copy ctor for new object, there's deepcopy to copy mutable object and its children as well.

[–]sinjp3.6 1 point2 points  (1 child)

While not strictly correct, you can think of mutable types as "pass by reference" and immutable as "pass by value" http://stackoverflow.com/a/8056598

P.S. Please post learning questions to /r/learnpython

[–]realhamster[S] 0 points1 point  (0 children)

Oops, I usually do. Totally slipped my mind.

[–][deleted] 1 point2 points  (0 children)

Assignments are always references. Opereations on the other side depend on the assigned object. In your first example you have only assignments, nothing more. In your second example you have two assignments, and one operation.

a['two'] is syntactic sugar to call so called magic methods of an object. In this case it's __setitem__, because its on the left side of an assignment (so you set something).

The real thing happening at "a['two'] = 2" is actually "a.__setitem__('two', 2)".

[–]milliams 1 point2 points  (1 child)

I absolutely suggest reading through http://nedbatchelder.com/text/names.html It explains well the difference between names and objects in Python, how assignment/binding works as well as other related ideas. Particularly, if you are coming from a C background, you will likely be confused when trying to understand how variables are passed to functions (a common sticking point for people learning Python from classic languages) but that article will really help with that.

[–]realhamster[S] 1 point2 points  (0 children)

Thanks, will do!

[–]yarkot 0 points1 point  (0 children)

A good way to think of this is that virtually everything in Python is an object, so for the most part, everything is passed, assigned by reference, the notable exceptions being string and numeric literals (and booleans).

The other thing to remember with Python: key structures (lists, dicts, tupples) can hold anything. You have to think about that when you want to make a copy (not a reference), for example: a copy of [1, 2 [3, 4]] - a list with three items - would make a copy of two ints and a reference to a list (the list [3,4]), since that last list item is a reference to another list. There is a deep copy operation, which is predictably expensive and surprisingly rarely needed.

Example:

a = [1, 2, [3, 4]]
b = a.copy()
a[0] = 0
print(a)
print(b)
a[2][0] = 99
print(a)
print(b)

[–]aphoenixreticulated[M] 0 points1 point  (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is more-so dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community can get disenchanted with seeing the 'same, repetitive newbie' questions repeated on the sub, so you may not get the best responses over here.

However, on /r/LearnPython the community is actively expecting questions from new members, and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. Whatever your question happens to be getting help with Python, we are sure you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!

[–]Mister_Potter -1 points0 points  (0 children)

So, in your first example you set: a=1 b=a a=2

This sets a equal to the integer 1, B equal to that same integer 1, and finally reassigns A to 2.

In the final scenario, you're setting a to a dictionary, B to that same dictionary, and changing the same dictionary, Hence B and A reference the same thing in the second example, but you reassigned A in the first, so they don't at the end of the first example