Class
A blueprint defining attributes and methods for objects.
Example: class Node:
defines a linked list node template.
Object (Instance)
Concrete creation from a class with unique attribute values.
Example: one = Node(1)
creates a Node object.
__init__
(Constructor)
Special method called when creating an object to initialize attributes.
Signature: def __init__(self, value, next=None)
self
Parameter
Reference to the current object instance (first parameter in methods).
Purpose: Access object's attributes like self.value
.
Attributes
Variables owned by an object (e.g., Node.value
, Node.next
).
Defined in constructor via self.attribute = ...
Base Case
Condition where recursion stops (no further calls).
Example: if head is None: return "None"
Recursive Case
Function calls itself with modified arguments to progress toward base case.
Example: return f"{head.value} -> {to_str(head.next)}"
Linked List
Recursive data structure where each Node
points to the next node or None
.
Components: head
(first node), tail
(last node, next=None
)
Recursive Traversal
Process: Handle current node → recursively process next
node.
Pattern: Access head.value
, then call function on head.next
.
Abstraction
Hiding complex implementation while exposing essential features.
OOP Aspect: Objects provide methods for interaction without revealing internals.
Instantiation
Creating an object from a class using ClassName()
.
Triggers __init__
to set initial attributes.
Stack Frame
Memory space for a function call. Recursion creates stacked frames.
Base case resolves first (top of stack).
RecursionError
Occurs if base case isn’t reached (infinite recursion).
Fix: Ensure recursive case progresses toward base case.
1. Class Definition
Front: What does this code define?
python
class Dog:
Back: A class (blueprint for objects).
Key: Look for class
keyword.
Front: Identify the instance attribute:
python
def __init__(self):
self.name = "Fido"
Back: self.name
(unique to each object).
Key: Any variable prefixed with self.
.
Front: What’s the class attribute?
python
class Dog:
species = "Canine"
Back: species
(shared by all Dog
objects).
Key: Variables declared inside class but outside methods.
Front: Which method is the constructor?
python
def __init__(self, age):
self.age = age
Back: __init__
(auto-called during instantiation).
Key: Always named __init__
.
Front: Is this a method or function?
python
def bark(self):
print("Woof!")
Back: Method (defined inside a class, takes self
).
Key: Functions are standalone; methods belong to classes.
Front: What does __str__
do?
python
def __str__(self):
return f"Dog: {self.name}"
Back: Defines how the object is printed (e.g., print(dog)
).
Key: Look for __method__
names.
Front: Find the local variable:
python
def calculate(self):
total = self.price * 2
Back: total
(exists only inside the method).
Key: Variables not attached to self
.
Front: What’s special here?
python
@classmethod
def change_species(cls, new_species):
cls.species = new_species
Back: Class method (modifies class-level attributes via cls
).
Key: Decorator @classmethod
and cls
parameter.
Front: How do you create a Dog
object?
Back: my_dog = Dog()
(calls __init__
).
Key: ClassName()
syntax.
Front: Why does every method need self
?
Back: self
refers to the current object instance.
Key: First parameter in instance methods.