How to fix issue with passing class instances between methods in Python (Django context)?
I am practicing Object-Oriented Programming in Python (Django context). I have two classes: one represents a ProjectIdea, and another represents a ProjectIdeaBoard.
I want to:
Add (
pin) aProjectIdeainstance to the boardRemove (
unpin) a specific instanceDisplay all ideas with their details
However, I’m confused about how to properly pass and manage instances between methods.
Here is my simplified code:
class ProjectIdea:
def __init__(self, title, description):
self.title = title
self.description = description
class ProjectIdeaBoard:
def __init__(self, title):
self.title = title
self.ideas = []
def pin(self, idea):
self.ideas.append(idea)
def unpin(self, idea):
self.ideas.remove(idea)
def count(self):
return len(self.ideas)
My Questions:
Is this the correct way to pass class instances between methods?
What is the best way to safely remove an object (avoid errors if not found)?
In Django, would this pattern be similar when working with models?
🔍 What I tried:
Using
pop()instead ofremove()Creating instance inside constructor (didn’t work as expected)
I want to understand the correct OOP approach for handling objects between classes, especially in real-world Django applications.