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()anddir() - Explain why two names can point at one object, and copy a list when you need a separate one
- Tell
isfrom==, and useisforNone - 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.
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.
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.
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.
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.
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.
| Python | Java | Rust | |
|---|---|---|---|
| What a name holds | A label pointing at an object. Every value, even 3, is an object | Numbers and true sit in the variable itself. Objects are reached through a reference, as in Python | The value itself. A reference is written out: &shelf |
b = a with a list | Two labels on one list | Two references to one list, as in Python | The list moves to b, and a can no longer be used (Simple Rust 3.1) |
| Same object or same value | is 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 away | When no name or list points at it any more. Python cleans it up for you | Cleaned up by the garbage collector, some time after nothing points at it | At 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.
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.
- The Python Language Reference: Objects, values and types: every object has an identity, a type and a value
- Built-in Functions: type
- Built-in Functions: dir
- Built-in Functions: id
- The Python Language Reference: Identity comparisons: is and is not
- Programming FAQ: Why did changing list ‘y’ also change list ‘x’?
- The Java Language Specification: 4.3 Reference Types and Values: Java names hold references to objects, and primitive values such as int directly
- The Java Language Specification: 12.6 Finalization of Class Instances: Java reclaims objects nothing refers to
- The Rust Programming Language: What Is Ownership?: Rust moves a value on assignment and drops it when its owner goes