def list(iterable: Optional[Iterable[Any]] = None, /) -> ListObject
Creates a persistent, mutable sequence object, analogous to a standard Python list.
Familiar API: This class is designed as a persistent counterpart to Python's built-in collection. It implements the same interface and supports all standard operations, allowing you to use it easily as a drop-in replacement while providing automatic persistance.
Parameters
iterableIterable[Any], optional
Optional iterable to initialize the list with.
Returns
A dbzero.list object.
Examples
Creating and modifying a list
You can create an empty list or initialize it from an iterable. Its API mirrors Python's built-in list.
# Create an empty list
tasks = db0.list()
# Append items
tasks.append("buy milk")
tasks.append("walk the dog")
assert len(tasks) == 2
assert tasks[1] == "walk the dog"
# Modify an item by index
tasks[0] = "buy almond milk"
assert tasks == ["buy almond milk", "walk the dog"]
# Remove an item
tasks.remove("walk the dog")
assert len(tasks) == 1List with other dbzero objects
A dbzero.list holds a reference to dbzero objects it contains.
class Task:
def __init__(self, description):
self.description = description
tasks_list = db0.list()
task1 = Task("write documentation")
tasks_list.append(task1)
# task1 is now being held by the list, extending it's life
# Later, removing the task from the list will also delete it
# (if no other references to it exist)
removed_task = tasks_list.pop()Slicing a dbzero.list
Slicing creates a standard Python list.
# Create a list of priority tasks
priorities = db0.list(["critical bug", "security patch", "feature request", "documentation", "refactoring"])
# Slicing creates a regular Python list
top_tasks = priorities[:3]
assert top_tasks == ["critical bug", "security patch", "feature request"]
assert type(top_tasks) is list
@db0.memo
class Sprint:
def __init__(self, tasks):
self.tasks = tasks
# When assigned to a memo object, it becomes a dbzero.list again
sprint = Sprint(top_tasks)
assert type(sprint.tasks) is db0.list