Unit 6 • Lesson 3

Creating and Using Objects

Overview

Objects are instances of classes that can store data and perform actions. You'll learn how to create objects, call their methods, and manage multiple instances that work independently, bringing your classes to life with actual data and behavior.

Beginner 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • Creating objects: Learn how to instantiate objects from classes.
  • Accessing attributes: Understand how to get and set object attributes.
  • Calling methods: Discover how to call methods on objects.
  • Multiple objects: Learn how to create and work with multiple independent objects.

Why This Matters

Defining a class is just the first step - you need to create objects from that class to actually use it! Once you create an object, you can access its attributes and call its methods. Each object is independent - creating multiple objects from the same class gives you multiple instances, each with its own data. This is the power of OOP!

Step 1: Creating Objects

To create an object (also called an instance), you call the class name like a function:

Creating Objects
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Create an object
person1 = Person("Alice", 25)

# This calls __init__ automatically
# person1 is now a Person object with name="Alice" and age=25
1

Call the Class

To create an object, write the class name followed by parentheses: Person(). Pass any required arguments.

2

__init__ Runs

Python automatically calls the __init__ method with the arguments you provided. This sets up the object's attributes.

3

Object Created

The object is created and returned. You can assign it to a variable to use it later!

Key Concept: Creating an object is like building a house from a blueprint. The class (blueprint) defines what the object will have, but calling Person("Alice", 25) actually creates a real Person object with those specific values!

Mini Practice #1: Create Objects

Try It Yourself

Create objects from a class:

Press Run to see output

What happened? You created two separate Dog objects! Each object has its own name and breed. dog1 and dog2 are independent - changing one doesn't affect the other. This is the power of objects - you can create as many as you need!

Step 2: Accessing Attributes

You can access and modify object attributes using dot notation:

Accessing Attributes
class Car:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color
        self.speed = 0

car1 = Car("Toyota", "Red")

# Access attributes
print(car1.brand)  # "Toyota"
print(car1.color)  # "Red"

# Modify attributes
car1.speed = 60
print(car1.speed)  # 60
1

Read Attributes

Use object.attribute to read an attribute's value. This gets the value stored in that object.

2

Modify Attributes

Use object.attribute = new_value to change an attribute. This updates that specific object's data.

3

Independent Objects

Each object has its own attributes. Changing one object's attributes doesn't affect other objects!

Dot Notation

The dot (.) is how you access anything that belongs to an object - attributes or methods. Think of it like saying "this object's thing" - car1.brand means "car1's brand attribute".

Mini Practice #2: Access and Modify Attributes

Try It Yourself

Work with object attributes:

Press Run to see output

What happened? You accessed the student's name and grade attributes using dot notation. Then you modified the grade attribute by assigning a new value. This shows how objects can store and update data!

Step 3: Calling Methods

Methods are functions that belong to objects. You call them using dot notation:

Calling Methods
class Car:
    def __init__(self, brand):
        self.brand = brand
        self.speed = 0
    
    def accelerate(self):
        self.speed += 10
    
    def get_info(self):
        return f"{self.brand} is going {self.speed} mph"

car1 = Car("Toyota")
car1.accelerate()  # Call the method
print(car1.get_info())  # "Toyota is going 10 mph"
1

Method Definition

Methods are defined inside the class using def. They always take self as the first parameter.

2

Call Methods

Use object.method_name() to call a method. Don't pass self - Python does that automatically!

3

Method Can Access Attributes

Methods can access and modify the object's attributes using self. They can also return values!

Step 4: Multiple Independent Objects

Each object is independent - creating multiple objects gives you multiple instances:

Multiple Objects
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

# Create multiple books
book1 = Book("Python Basics", "John Doe")
book2 = Book("Advanced Python", "Jane Smith")

# Each object is independent
print(book1.title)  # "Python Basics"
print(book2.title)  # "Advanced Python"

# Changing one doesn't affect the other
book1.title = "New Title"
print(book1.title)  # "New Title"
print(book2.title)  # "Advanced Python" (unchanged!)

Remember: Each object is a separate instance with its own data. Changing one object's attributes doesn't affect other objects created from the same class. This independence is what makes OOP powerful - you can work with many objects simultaneously!

End-of-Lesson Exercises

Exercise 1: Create and Access Objects

Create a Person class with __init__ that takes name and age. Create two Person objects: "Alice" age 25 and "Bob" age 30. Print both people's names and ages.

Define class Person, create two objects, then print their attributes.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Modify Object Attributes

Create a Counter class with __init__ that sets self.count = 0. Create a Counter object, then modify its count to 5, and print the count.

Define class Counter, create object, modify count attribute, print it.

Write your code above and click "Check Answer" to verify it's correct.