Course contentsShow
← Python
Lesson 411 of 1,0607. Object-Oriented ProgrammingFree lesson

Instantiating Objects from Class Blueprints

Learners will create individual object instances from a defined class to represent distinct entities within their program memory.

Instantiating Objects from Class Blueprints

In this lesson, you will learn how to breathe life into your class definitions by creating unique object instances that reside in your program’s memory.

From Blueprint to Reality

Think of a class as an architectural blueprint for a house. The blueprint itself isn't a house; you cannot live in a piece of paper. It simply defines the structure—how many rooms it has and where the walls go. Instantiation is the process of using that blueprint to construct an actual, physical house.

In Python, the class serves as the blueprint, and the object is the individual entity created from it. Every time you instantiate a class, Python allocates a new space in memory for that specific object, allowing it to hold its own unique data without interfering with other objects created from the same class.

Creating Instances

To instantiate a class, you use the class name followed by parentheses (), just like calling a function. You typically assign this new object to a variable so you can reference it later.

Consider this example:

class Robot:
    def __init__(self, name):
        self.name = name

# We are using the 'Robot' blueprint to create two distinct entities
bot_one = Robot("Alpha")
bot_two = Robot("Beta")

print(bot_one.name)  # Output: Alpha
print(bot_two.name)  # Output: Beta

In the code above, Robot is the blueprint. By calling Robot("Alpha") and Robot("Beta"), we created two separate objects in memory. Even though they share the same structure and logic defined in the Robot class, bot_one and bot_two are entirely independent. Changing the name of bot_one would have no impact on bot_two.

By instantiating objects, you transform abstract code definitions into functional data entities that your program can manipulate, track, and interact with individually. This is the foundation of building complex systems where many different "things" need to track their own state simultaneously.

Key Takeaway: Instantiating a class creates a unique, independent object in memory, allowing you to manage multiple distinct entities using the same shared blueprint.