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.
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:
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
Call the Class
To create an object, write the class name followed by parentheses: Person(). Pass any required arguments.
__init__ Runs
Python automatically calls the __init__ method with the arguments you provided. This sets up the object's attributes.
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 YourselfCreate objects from a class:
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:
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
Read Attributes
Use object.attribute to read an attribute's value. This gets the value stored in that object.
Modify Attributes
Use object.attribute = new_value to change an attribute. This updates that specific object's data.
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 YourselfWork with object attributes:
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:
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"
Method Definition
Methods are defined inside the class using def. They always take self as the first parameter.
Call Methods
Use object.method_name() to call a method. Don't pass self - Python does that automatically!
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:
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.
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.