domains / / crewaiinc/crewai
Default None not empty
Always use `None` as the default value for optional parameters and class attributes instead of empty collections (`[]`, `{}`, `""`) or other mutable defaults. This prevents sharing mutable state between instances and makes null checks more explicit.
Always use None as the default value for optional parameters and class attributes instead of empty collections ([], {}, "") or other mutable defaults. This prevents sharing mutable state between instances and makes null checks more explicit.
Example:
# ❌ Bad - Using mutable defaults
class ToolAdapter:
original_tools: List[BaseTool] = [] # Shared between instances
def __init__(self, items: List[str] = []): # Mutable default
self.items = items
# ✅ Good - Using None as default
class ToolAdapter:
def __init__(self):
self.original_tools: List[BaseTool] = [] # Instance-specific
def process(self, items: Optional[List[str]] = None):
items = items or [] # Convert None to empty list
# Process items...
This approach:
- Prevents shared state bugs from mutable defaults
- Makes optional parameters explicit
- Forces conscious initialization of collections
- Enables clear null checking patterns