Constructor parameters in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how the time it takes to create an object changes when we pass different parameters to its constructor.
We want to know: does adding more or bigger parameters make creating the object slower?
Analyze the time complexity of the following code snippet.
class Box:
def __init__(self, items):
self.contents = []
for item in items:
self.contents.append(item)
box = Box([1, 2, 3, 4, 5])
This code creates a Box object and copies each item from the input list into the box's contents.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each item in the input list.
- How many times: Once for every item in the input list.
As the number of items increases, the loop runs more times, copying each item.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 copies |
| 100 | About 100 copies |
| 1000 | About 1000 copies |
Pattern observation: The work grows directly with the number of items; double the items, double the work.
Time Complexity: O(n)
This means the time to create the object grows in a straight line with the number of items passed in.
[X] Wrong: "Constructor parameters don't affect how long object creation takes."
[OK] Correct: If the constructor processes each parameter, more or bigger parameters mean more work and more time.
Understanding how constructor parameters affect time helps you explain object creation costs clearly and confidently in real projects.
What if the constructor used a built-in copy method instead of a loop? How would the time complexity change?
Practice
Solution
Step 1: Understand what constructor parameters do
Constructor parameters allow you to pass values to an object when you create it, so it starts with specific data.Step 2: Compare options with this understanding
Only To provide initial values to an object when it is created describes this purpose correctly. Other options describe unrelated concepts.Final Answer:
To provide initial values to an object when it is created -> Option AQuick Check:
Constructor parameters = initial values [OK]
- Confusing constructor parameters with methods
- Thinking constructor parameters delete objects
- Believing constructor parameters create global variables
Solution
Step 1: Recall Python constructor syntax
Python uses the special method named __init__ with self as the first parameter to define constructors.Step 2: Check each option
Only def __init__(self, name): uses the correct method name and includes self and a parameter.Final Answer:
def __init__(self, name): -> Option DQuick Check:
Constructor method = __init__ with self [OK]
- Using wrong method names like constructor or init
- Omitting self parameter
- Using incorrect special method names
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} says Woof!"
my_dog = Dog("Buddy")
print(my_dog.speak())Solution
Step 1: Understand constructor parameter usage
The constructor takes a name and assigns it to self.name. The object my_dog is created with name "Buddy".Step 2: Analyze the speak method output
The speak method returns a string using self.name, so it returns "Buddy says Woof!".Final Answer:
Buddy says Woof! -> Option AQuick Check:
Object name used in speak = Buddy says Woof! [OK]
- Ignoring the name parameter and expecting just 'Woof!'
- Confusing class name with instance name
- Assuming missing parameters cause error here
class Car:
def __init__(color):
self.color = color
my_car = Car("red")Solution
Step 1: Check __init__ method parameters
The first parameter of __init__ must be self to refer to the instance. Here, self is missing.Step 2: Confirm other parts are correct
Method name __init__ is correct, assignment to self.color is valid, and "red" is properly quoted.Final Answer:
Missing self parameter in __init__ method -> Option BQuick Check:
__init__ first parameter = self [OK]
- Forgetting self parameter
- Changing __init__ method name
- Misunderstanding self usage
Book that stores title and author when you create a new book object. Which constructor correctly sets these attributes?Solution
Step 1: Check parameter list and self usage
The constructor must have self as first parameter, then title and author. def __init__(self, title, author): self.title = title self.author = author has this correct.Step 2: Verify attribute assignment
Attributes must be assigned as self.title = title and self.author = author to store values in the object. def __init__(self, title, author): self.title = title self.author = author does this correctly.Final Answer:
def __init__(self, title, author): self.title = title self.author = author -> Option CQuick Check:
Use self to assign attributes in __init__ [OK]
- Omitting self parameter
- Assigning parameters to themselves instead of self
- Reversing assignment order
