Classes and objects · lesson 1 of 7
Parts in this lesson
Atype(x)what kind of object
Bdir(x)what it can do
Ca = ba second label, not a copy
Da is bthe very same object?
Emutable, immutablecan it change?

Everything is an object

Last updated

You've been using objects since your first print. Every value in Python is one: a piece of data that knows its type and what it can do. In this lesson you'll ask a value what it is, see that a name is a label stuck on an object, not a box holding it, find out why changing one list can seem to change another, and learn which objects can change and which never do.

By the end of this lesson you can

  • Say what an object is, and ask one what it is with type() and dir()
  • Explain why two names can point at one object, and copy a list when you need a separate one
  • Tell is from ==, and use is for None
  • Say which objects can change and which never do

You've used objects since 1.1

Every value in Python is an object: a piece of data that knows what kind of thing it is, and comes with things it can do. You've used them all along. "dune".upper() asks a piece of text to give back a shouting copy of itself. books.append("Emma") asks a list to add to itself. The dot means "ask this object to do something".

type() tells you what kind of object a value is. Python calls a kind of object a class: text is class str, a list is class list.

Step 1 code main.py
Edit the code, then run it again

Even numbers are objects: line 7 asks 2.5 whether it's a whole number. And so is print itself, which is why type(print) has an answer. In the next lesson you'll write a class of your own, Book, and make objects from it.

Asking an object what it is

dir() lists everything an object can do. The list is long, and the names starting with _ are Python's own machinery, so line 3 keeps only the others, with a comprehension from lesson 2.4. Line 4 shows the first six. upper is in there, which is why title.upper() works.

Every object also has an identity: a number that's different for every object alive at the same time. id() gives it to you. The number itself means nothing and changes every run, so this program only compares two of them: line 8 asks whether other and title are the very same object.

Step 2 code main.py
Edit the code, then run it again

Change line 4 to print(names) to see all of them. Then try dir(["Dune"]) and find append.

Names are labels, not boxes

Here's what catches out almost everyone. The library has a shelf of books, and makes a display list for the window, starting with the same books. One more book goes on display. Run it and look at the shelf.

Step 3 code main.py
Edit the code, then run it again

A name in Python is a label on an object, not a box that holds one. display = shelf means "stick the label display on whatever shelf points at". Watch it run: the values table shows both names, and after line 3 both show the new book.

Lesson 2.5 met the same thing: the list used as a default value was one list, shared by every call.

is and ==

Two questions you can ask about two names:

  • a == b: do they have the same value? Two lists with the same books in the same order are equal.
  • a is b: are they the very same object? That's true only when both labels are on one object.
Step 4 code main.py
Edit the code, then run it again

a and b are equal but separate, so adding to b on line 7 leaves a as it was. a and c are one list.

Almost always you want ==. The one place to use is is with None: there's only ever one None object, so loan is None is the usual way to ask "is there nothing here?"

Objects that can change, and objects that can't

Some objects can change after they're made: lists, dicts and sets. Python calls them mutable. Others never change: text, numbers, tuples, True, False and None. They're immutable.

Step 5 code main.py
Edit the code, then run it again

Line 2 doesn't change title. Text can't change, so .upper() makes a new piece of text. Line 6 doesn't change the number 2 either. count += 1 works out 3, a new object, and moves the label count onto it. before still points at 2.

A list is different. shelf.append() on line 10 changes the list itself, so every label on it sees the change.

That's why two labels on one object only cause surprises with mutable objects. When you hand a list to a function or keep it in another object, both share it, and either can change it. You'll use that on purpose in lesson 3.3, and guard against it in Stage 4.

In other languages

This step is optional. It's for anyone who will meet Java or Rust later.

Names and objects in three languages
PythonJavaRust
What a name holdsA label pointing at an object. Every value, even 3, is an objectNumbers and true sit in the variable itself. Objects are reached through a reference, as in PythonThe value itself. A reference is written out: &shelf
b = a with a listTwo labels on one listTwo references to one list, as in PythonThe list moves to b, and a can no longer be used (Simple Rust 3.1)
Same object or same valueis and ==== on objects asks "same object?", and .equals() asks "same value?"== asks "same value?". There is no "same object?" for ordinary values
When an object goes awayWhen no name or list points at it any more. Python cleans it up for youCleaned up by the garbage collector, some time after nothing points at itAt the end of the block that owns it, decided while compiling

Java behaves much like Python for objects, but keeps plain numbers out of it. Rust is the odd one out. Its rules about who owns a value are taught in Simple Rust.

Check what you learned

Three questions. Pick or type an answer, then check it.

What does this program print?
loans = ["Dune"]
today = loans
today.append("Emma")
print(len(loans))
a = [1, 2] and b = [1, 2]. Which is true?
Which of these can change after it's made?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python