Module 11 · Object-Oriented Programming — topic 3 of 25
Objects
Instances in detail: identity, type and value, creating many independent objects, references and aliasing, comparing objects with == and is, and object lifetime.
Learning objectives
- Create and use multiple independent objects
- Explain aliasing when two variables refer to the same object
- Compare objects by identity and by value
Definition
An object is a concrete instance of a class. Every object has an identity (id()), a type (type()) and a value (its attributes); variables hold references to objects rather than the objects themselves.
Introduction
Each call to a class creates a fresh object with its own data. Assigning that object to another variable doesn't copy it — both names point to the same object.
Why it matters
Aliasing explains many 'why did my other variable change?' bugs, and knowing how equality works for your objects is essential for searching lists and using objects in sets.
Detailed explanation
Every object has an id() that is unique while it exists, a type, and state stored in its attributes.
Assignment binds a name to an object: b = a makes both names refer to the same object. Changing b.balance changes a.balance too.
is compares identity (same object); == compares value. By default, == on your own objects falls back to identity, so two separate Point(1, 2) objects are not equal unless you define __eq__.
Use copy.copy() or a method that builds a new object when you need an independent copy.
Objects live as long as something refers to them. When the last reference disappears, Python frees the object automatically (garbage collection).
Syntax
- Variables hold references.
- is checks identity, == checks equality.
- Define __eq__ for value equality.
Code examples
Independent objects
2 1 False
Aliasing
10 True True
More examples
Equality for your own objects
False True False True 5 9 False
Step-by-step explanation
- Point has no __eq__, so == compares identity: two separate objects are not equal.
- ValuePoint defines __eq__ to compare coordinates.
- in uses ==, so the search finds an equal point even though it's a different object.
- copy.copy makes a new object; changing q doesn't affect p.
Expected output
Running “Equality for your own objects” prints exactly:
Practical applications
- Two variables pointing at the same shopping cart object see the same items.
- ORMs return one object per database row; identity matters for caching.
Best practices
- Use is only for None and identity checks.
- Define __eq__ when objects represent values.
- Copy explicitly when you need independence.
Common mistakes
Expecting assignment to copy
Avoid
Prefer
Assignment only creates another reference.
Using is for value comparison
Avoid
Prefer
is is true only for the very same object.
Important notes
- If you define __eq__, Python sets __hash__ to None, making instances unhashable unless you also define __hash__ (see Special methods).
Key takeaways
- Each instance has its own identity and state.
- Assignment creates aliases, not copies.
- is compares identity; == compares value via __eq__.
Try it yourself
- 1Create two Student objects with the same data and show the difference between == and is.
- 2Add __eq__ to a Book class so books with the same ISBN are equal.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
16 questions cover the “Object-Oriented Programming” module. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?
- ModerateWhat is printed?
- ModerateWhat is printed?