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:
Enter the URL of a public GitHub repository