Python 3D Academyবাংলা

Names point to objects

🌍 In real life. A cricket bat lies in the club room. One player calls it "Rimi's bat" and another calls it "the team bat". Two names, one bat: if someone tapes the handle, both names now refer to a taped bat. Giving the bat a second name didn't make a second bat.

Python names work exactly like that.

The idea in one sentence: a variable is a name tag with an arrow to an object, so b = a doesn't copy anything: it ties a second tag to the same object.

Its words:

  • object: a value living in memory, like a list
  • reference (the arrow): what a name holds: where its object is
  • alias: a second name for the same object
  • == asks "equal values?"; is asks "the same object?"

Step by step, for a = [1, 2], b = a, b.append(3):

  1. a = [1, 2] makes a list and points the tag a at it.
  2. b = a points a second tag, b, at the same list.
  3. b.append(3) changes that one list, so a sees [1, 2, 3] too.

In earlier chapters we drew variables as boxes. That picture works for numbers and strings, but for lists the arrow picture is the true one.

a = [1, 2]
b = a
b.append(3)
print(a)
print(a is b)
We only changed the list through b, yet a shows the change: a and b are two names for one list.

Two different tests:

  • a == b asks "do they hold equal values?"
  • a is b asks "are they the same object?"

Two separate lists can be equal without being the same list.

a = [1, 2]
b = [1, 2]
c = a
print(a == b, a is b)
print(a == c, a is c)

id(x) gives the object's identity number. The number itself changes from run to run, but two names point to the same object exactly when their ids are equal:

a = [1, 2]
b = [1, 2]
c = a
print(id(a) == id(c))
print(id(a) == id(b))

Why Python works this way. Copying a big list every time you give it a second name would be slow and would waste memory. Pointing a new tag at the same object is instant, whatever the size. The price is that you have to know when two names share an object.

Mistakes and edge cases.

  • "Backing up" with =. backup = original makes an alias, not a backup: changes to one show up in the other (see the bug question). Lesson 13.3 shows real copies.
  • is for values. [1] is [1] is False: two separate lists, even though they're equal.
  • Expecting the same id every run. id() numbers change between runs; only compare ids within one run.

Quiz

  1. What does this print?

    a = [1]
    b = a
    b.append(2)
    print(a)
  2. What does this print?

    a = [1]
    b = [1]
    print(a is b)
  3. What does this print?

    a = [1]
    b = [1]
    print(a == b)
  4. What does b = a do when a is a list?

    • Makes b another name for the same list
    • Makes a copy of the list for b
    • Moves the list from a to b
  5. What does this print?

    a = [1, 2]
    c = a
    c[0] = 9
    print(a[0])
  6. backup should still be [1, 2], but this prints [1, 2, 3]. Which line causes it?

    original = [1, 2]
    backup = original
    original.append(3)
    print(backup)

Coding tasks

  1. Start with a = [1, 2]. Make b another name for the same list (not a copy), append 3 to it through b, then print a.

  2. Here are two lists: a = [1, 2] and b = [1, 2]. On one line, print the result of a == b, then the result of a is b.

  3. Ask for whole numbers on one line and put them in a list called original. Write alias = original. Then double every number through alias (change alias[i] for each position), and print original.