Objects -- Creating Instances
Bring your classes to life by creating objects from them.
Introduction
In the previous lesson, you learned how to define a class -- a blueprint. But a blueprint by itself does not do anything useful. You need to actually build something from it. That is what objects are: real, usable instances created from a class.
What is an Object?
An object is a specific instance created from a class. If the class is the blueprint for a house, then the object is the actual house you build and live in.
Key insight: You can build many houses from the same blueprint. Similarly, you can create many objects from the same class. Each object is independent -- it has its own copy of the data.
Creating Objects
To create an object, you call the class name followed by parentheses, just like calling a function:
class Car:
brand = "Toyota"
color = "Red"
# Creating objects (instances) from the Car class
car1 = Car()
car2 = Car()
print(car1.brand) # Toyota
print(car2.color) # Red
Here, car1 and car2 are two separate objects, both created from the same Car class.
Multiple Objects from One Class
You can create as many objects as you need from a single class:
class Dog:
name = "Unknown"
breed = "Mixed"
dog1 = Dog()
dog2 = Dog()
dog3 = Dog()
# Each is a separate object
print(dog1) # <__main__.Dog object at 0x...>
print(dog2) # <__main__.Dog object at 0x...>
print(dog3) # <__main__.Dog object at 0x...>
Notice that each object has a different memory address -- they are three distinct dogs, even though they were all created from the same Dog class.
Each Object is Independent
Changing one object does not affect others:
class Dog:
name = "Unknown"
dog1 = Dog()
dog2 = Dog()
dog1.name = "Buddy"
dog2.name = "Max"
print(dog1.name) # Buddy
print(dog2.name) # Max
Even though both came from the same class, dog1 and dog2 have their own separate data. Changing Buddy's name does not change Max's.
The type() and isinstance() Functions
Python gives you tools to check what type an object is:
class Car:
brand = "Toyota"
my_car = Car()
# Check the type of an object
print(type(my_car)) # <class '__main__.Car'>
# Check if an object is an instance of a class
print(isinstance(my_car, Car)) # True
print(isinstance(my_car, int)) # False
OOP Terms
- Object: A specific instance created from a class. It holds actual data and can perform actions.
- Instance: Another word for object. "Creating an instance" and "creating an object" mean the same thing.
- Instantiation: The process of creating an object from a class.
Real-World Connection
Think about Amazon. They have one Product class that defines what a product looks like -- name, price, description, rating. But from that single class, they create millions of product objects, one for each item in their store. Each object has its own unique data.
Best Practices
- Give your object variables meaningful names:
my_caris better thanx. - Remember that each object is independent -- changes to one do not affect others.
- Use
type()andisinstance()when you need to verify what kind of object you are working with.
Common Mistakes
- Forgetting the parentheses:
car1 = Cardoes not create an object -- you needcar1 = Car(). - Confusing a class with an object. The class is the template; the object is the actual thing.
- Assuming that modifying one object changes all objects of the same class.
Practice Exercises
- Create a
Fruitclass with anameattribute. Create three fruit objects and give each a different name. - Create a
Phoneclass, create two objects, and usetype()andisinstance()to verify their type. - Create a
Colorclass with anameattribute. Create two objects, change one's name, and print both to confirm they are independent.
Summary
- An object is a specific instance created from a class.
- Create objects by calling the class name with parentheses:
obj = ClassName(). - You can create multiple objects from the same class.
- Each object is independent -- modifying one does not affect others.
- Use
type()andisinstance()to check an object's type.