R

ReflexKR

Reflex Docs

Name Tag Airport: Python's Object Model

·

Course overview

1

A tag is not the bag: names and objects

2

Sealed bags: mutable and immutable

3

Bags inside bags: containers and copying

4

Handing over a bag: functions and arguments

5

Zones and passes: scope

6

Carry-on bags: closures

7

The bag spec: classes and instances

8

Standard airport procedures: dunder methods

9

The spec behind the spec: inheritance and MRO

10

The checkpoint: how attribute access really works

11

Unclaimed bags: memory and GC

12

Locker numbers: hashing, equality and the big picture

Name Tag Airport: Python's Object Model
Lesson 1

Time 0:00–0:50

A tag is not the bag: names and objects

Lost-baggage report

LOST-001

I thought there were two bags

Reported by

Park, assistant manager at a travel agency

What went missing

A group souvenir list. A magnet was added to the list 'copied' as a backup, and the magnet turned up in the original list too.

To close the case

Prove in code that the two lists were the same bag from the start, and make a real backup list.

Haneulseom International Airport · Baggage Center

It is the morning of your first day. Supervisor Do Miran hands you a bundle of tags in front of the conveyor. "There's one thing you learn before anything else here. One bag, many tags."

The first report comes from a travel agent. They made a second copy of a souvenir list as a backup, but whatever they write on the backup also appears on the original. The supervisor glances at the code and smiles. "Two lists? Two tags, more like."

1-1. = hangs a tag (9 min)

Here is the reporter's code.

souvenirs = ["postcard", "keyring"]
backup = souvenirs
backup.append("magnet")
print(souvenirs)
# ['postcard', 'keyring', 'magnet']

backup = souvenirs does not copy the list. It hangs one more tag, backup, on the same bag that already carries the souvenirs tag. There was only ever one bag, and backup.append put the magnet into that one bag.

  • = first evaluates the right-hand side to get an object (a bag), then binds the name on the left (a tag) to that object.
  • If the right-hand side is an existing name, no new object is created. The same object simply gets one more name.

Step through the tag board below one line at a time. The left column holds name tags, the right holds bags. Before line 3 you have to predict the result to move on.

Tag board

Step 1 / 0

Next to run: 0

Preparing the lab…

Just changed

Sealed bag (immutable)

Even after line 2 there is only one bag. Check that both arrows point to the same list.

1-2. Three things every bag has: identity, type, value (8 min)

Every object has three things.

How to checkAt the airport
Identityid(obj), a is bThe unique number stamped on the bottom of the bag
Typetype(obj)The kind of bag (suitcase, backpack…)
ValueCompare with ==What is packed inside
x = ["postcard"]
y = ["postcard"]
print(x == y, x is y)
# True False
print(id(x) == id(y), id(souvenirs) == id(backup))
# False True
  • x and y are two different bags with identical contents. == is True, is is False.
  • souvenirs and backup are the same bag, so their id is the same too.
  • The number returned by id means nothing on its own. In CPython it is a memory address, and it changes from run to run. Use it only for comparisons.
print(type(x), type(len))
# <class 'list'> <class 'builtin_function_or_method'>

Pin says

→ is asks 'same bag?', == asks 'same contents?'. To compare values, always use ==. Save is for objects there is only one of, like x is None.

1-3. Everything is a bag (6 min)

Numbers and lists are not the only objects. Functions, classes and modules are objects bound to names too.

shout = print
shout("boarding")
# boarding
print(shout is print)
# True

def greet():
    return "hi"

print(type(greet), greet.__name__)
# <class 'function'> greet
alias = greet
del greet
print(alias())
# hi
  • def greet(): creates a function object and binds the name greet to it. It is a kind of assignment.
  • Removing the name with del greet leaves the function alive, because alias still points to the same function object.
  • Classes are objects as well. Both type(int) and type(type) are <class 'type'>. We come back to this in Lesson 7.

1-4. Moving a tag vs. changing the bag (11 min)

There are only two things you can do with a name.

  1. Move the tag (rebinding): a = .... The name is bound to a different object. The original bag stays as it was.
  2. Change the bag (mutation): a.append(...), a[0] = .... The name stays put and the bag's contents change. Every name pointing to that bag sees the change.
a = [1, 2]
b = a
a = a + [3]
print(a, b, a is b)
# [1, 2, 3] [1, 2] False
a = [1, 2]
b = a
a.append(3)
print(a, b, a is b)
# [1, 2, 3] [1, 2, 3] True

a + [3] builds a new list, and a = moves the tag a onto that new list. b stays on the original bag. append, on the other hand, changes the bag itself, so b sees it too. Switch between the two versions below and run each one.

Tag board

Step 1 / 0

Next to run: 0

Preparing the lab…

Just changed

Sealed bag (immutable)

With a = a + [3], the arrow a moves to a new bag after line 3. With a.append(3), the arrow stays put and only the inside of the bag changes.
  • In the list drawing, the 1 and 2 in the slots are small values, so they are written as values instead of arrows. They are objects too, though, and the list really holds references to them (Lesson 3).
  • Which kind is a += [3]? For lists it changes the bag. For numbers and strings, however, it moves the tag. These two faces of += are the topic of Lesson 2.

1-5. del removes the tag, not the bag (6 min)

c = ["tag"]
d = c
del c
print(d)
# ['tag']
print(c)
# NameError: name 'c' is not defined

del c only removes the name c; it does not delete the list. d still points to it, so the bag survives. A bag actually disappears only when no tag points to it anymore (Lesson 11).

Swapping two names with p, q = q, p is also tag moving. The right-hand side q, p builds a tuple first, and then the names on the left are bound to the objects inside it, one by one.

p, q = "left", "right"
p, q = q, p
print(p, q)
# right left

Case closed (10 min)

Here is the proof that the reporter's two lists were the same bag, and how to make a real backup list.

souvenirs = ["postcard", "keyring"]
backup = souvenirs
backup.append("magnet")
print(souvenirs is backup, id(souvenirs) == id(backup))
# True True

safe = list(souvenirs)     # make a new bag and pack the same contents into it
safe.append("mug")
print(souvenirs, safe, safe is souvenirs)
# ['postcard', 'keyring', 'magnet'] ['postcard', 'keyring', 'magnet', 'mug'] False

list(souvenirs) creates a new list. Now you can put a mug into safe and the original list stays the same. (This copy only rebuilds "one level"; Lesson 3 covers what that means.)

Near the end of the shift, Pin points toward conveyor 13. Someone has reported two bags with no tags at all, still going around and around. The supervisor only says, "With no tags, they should have been cleared away long ago," and switches off the screen.

Closing report — LOST-001. There was only ever one list, with two tags. Confirmed with is and id; solved by making a new list with list().

Exercises

  1. If you run x = [0], y = x, x = [1] in that order, what is y? Draw the picture first, then run it to check.
  2. With a = [1, 2], create b = a[:] and c = a, then predict the results of a is b, a is c and a == b.
  3. What happens if you give print another name and then run del print? You will learn where built-in names live in Lesson 5.

The supervisor's notebook

  • A variable is a name tag, not a box. = binds the name on the left to the object on the right.

  • Three things every object has: identity (id, is), type (type), value (==).

  • is asks whether it is the same object, == whether the values are equal. Always compare values with ==.

  • Functions, classes and modules are objects too. def is also an assignment that binds a name.

  • Rebinding (a = …) moves the name; mutation (a.append …) changes the object.

  • del only removes a name; an object disappears when nothing points to it anymore.

← Previous

Course overview

Next →

Lesson 2 · Sealed bags: mutable and immutable

Get news about new lessons

We'll email you when this course gets new lessons or a follow-up course opens.

We collect: your email address · Purpose: news about new lessons or the launch of this course · Kept until: you ask us to remove it. We never use it for anything else or share it. Reply to any notice email and we'll delete it right away.

Questions & answers

Ask anything about this lesson. Questions from every language version of this page appear together.

Shown as plain text, without links or formatting.

R

ReflexKR

Learn Python and Reflex by doing. Every example was run and verified on Reflex 0.10.0.

ReflexKR is free, with no banner ads. If it helped you, you can support it with a coffee. ☕ Buy us a coffee

Affiliate disclosure
한국어English日本語简体中文EspañolDeutsch

© 2026 ReflexKR · Since 2026 · CC BY-NC 4.0

Built with Reflex