Skip to content

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.

Beginner 10 min read·Lesson 160 of 306 in Python Programming

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

Python
a = Point(1, 2)
b = a # alias: same object
c = Point(1, 2) # a different object
a is b, a is c # True, False
a == c # False unless __eq__ is defined
  • Variables hold references.
  • is checks identity, == checks equality.
  • Define __eq__ for value equality.

Code examples

Independent objects

Python
class Counter:
def __init__(self):
self.count = 0
def click(self):
self.count += 1
a, b = Counter(), Counter()
a.click(); a.click(); b.click()
print(a.count, b.count, a is b)
Output
2 1 False

Aliasing

Python
class Counter:
def __init__(self):
self.count = 0
first = Counter()
second = first
second.count = 10
print(first.count, first is second, id(first) == id(second))
Output
10 True True

More examples

Equality for your own objects

Python
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
class ValuePoint:
def __init__(self, x, y):
self.x, self.y = x, y
def __eq__(self, other):
return isinstance(other, ValuePoint) and (self.x, self.y) == (other.x, other.y)
print(Point(1, 2) == Point(1, 2))
print(ValuePoint(1, 2) == ValuePoint(1, 2), ValuePoint(1, 2) == ValuePoint(2, 1))
points = [ValuePoint(0, 0), ValuePoint(3, 4)]
print(ValuePoint(3, 4) in points)
import copy
p = ValuePoint(5, 5)
q = copy.copy(p)
q.x = 9
print(p.x, q.x, p == q)
Output
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:

TextOutput
False
True False
True
5 9 False

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

backup = settings
settings.volume = 0 # backup changed too

Prefer

import copy
backup = copy.copy(settings)

Assignment only creates another reference.

Using is for value comparison

Avoid

if p is Point(1, 2): ...

Prefer

if p == Point(1, 2): ... # with __eq__ defined

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

  1. 1
    Create two Student objects with the same data and show the difference between == and is.
  2. 2
    Add __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?
Practise this module