The assignment y = x makes a copy of the object referred to by x, then makes y reference it.


Assignment copies the object


Assignment copies the reference to the object

Here is what's right.

Variable assignment copies the reference to the object (“identity” of the object) rather than creating a copy of the object and referencing the new copy. For example:

table_tennis = Sport()
ping_pong = table_tennis

The first line of the above code creates a Sport object and stores the reference to that object in variable table_tennis. The second line copies the reference that is stored in table_tennis into variable ping_pong. At the end of this code, there still is only a single Sport object. And there are two variables, table_tennis and ping_pong, that both refer to that same object.

Indeed, calling id, that returns the “identity” of the object referred to by a variable, on both table_tennis and ping_pong shows the same value.

How can you build on this misconception?

Read through Python’s tutorial on Classes to learn more.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.