Computer Science
Grade 9
20 min
The `__init__` Method: Initializing Objects
Understand the purpose and use of the `__init__` method for object initialization.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define the purpose of the `__init__` method as a class constructor.
Explain the role of the `self` parameter in referencing the current object instance.
Write a class with an `__init__` method that accepts parameters to set initial attribute values.
Create multiple unique objects (instances) from a single class by passing different arguments during instantiation.
Differentiate between parameters passed to `__init__` and the instance attributes they initialize.
Debug `TypeError` exceptions caused by missing or incorrect arguments when creating an object.
How does a new video game character get its starting name, health, and inventory the moment it's created? 🎮 The `__init__` method is the magic that sets up everything!
In this lesson, you will learn...
2
Key Concepts & Vocabulary
TermDefinitionExample
ClassA blueprint or template for creating objects. It defines a set of attributes and methods that the created objects will have.`class Dog:` is a blueprint for creating individual dog objects.
Object (Instance)A specific entity created from a class. It has its own unique state (values for its attributes).`my_pet = Dog()` creates an object named `my_pet` which is an instance of the `Dog` class.
__init__ MethodA special method in a class that is automatically called whenever a new object of that class is created. Its primary job is to initialize the object's attributes.`def __init__(self, name):` is the start of the initializer for a class that takes a name.
self ParameterThe first parameter of any method within a class, including `__init__`. It represents the sp...
3
Core Syntax & Patterns
The `__init__` Method Syntax
def __init__(self, param1, param2, ...):
self.attribute1 = param1
self.attribute2 = param2
The method must be named exactly `__init__` with double underscores on both sides. The first parameter must always be `self`. Additional parameters are used to accept data when an object is created, which are then assigned to instance attributes using `self.attribute_name = value`.
Object Instantiation Pattern
my_object = ClassName(argument1, argument2, ...)
To create an object, you call the class name like a function. The arguments you provide in the parentheses are passed directly to the `__init__` method's parameters (Python handles `self` automatically). The number of arguments must match the number of parameters in `__init__` (excluding `s...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You are debugging this code: `class Product: def __init__(self, name, price): self.name = name; price = price`. After running `p = Product("Phone", 800)`, trying to access `p.price` causes an `AttributeError`. What is the logical error in the `__init__` method?
A.The parameter `price` should be named `product_price`.
B.The `__init__` method cannot have more than two parameters.
C.The class name `Product` is invalid.
D.The line `price = price` should be `self.price = price`.
Challenging
A `Circle` class needs to be initialized with a `radius`. Upon creation, it should also calculate and store its `area` (use 3.14 for pi). Which `__init__` method correctly implements this?
A.def __init__(self, radius):
self.radius = radius
self.area = 3.14 * radius * radius
B.def __init__(self, radius, area):
self.radius = radius
self.area = area
C.def __init__(self, radius):
self.radius = radius
area = 3.14 * self.radius * self.radius
D.def __init__(self, radius):
self.radius = radius
def calculate_area(self):
self.area = 3.14 * self.radius * self.radius
Challenging
Given `class Robot: def __init__(self, model_num): self.model = model_num`, which of the following lines of code will *not* cause a `TypeError`?
A.r1 = Robot()
B.r1 = Robot("R2D2", "C3PO")
C.r1 = Robot(9000)
D.r1 = Robot.create("T-800")
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free