dbzero.tuple(iterable=())
Creates a persistent, immutable sequence object that behaves like Python's built-in tuple. This object is managed by dbzero, making its contents and the relationships between them part of the persistent data layer.
Parameters
| Name | Type | Description |
|---|---|---|
iterable | object | Optional. An iterable (e.g., a list, tuple, or generator) used to initialize the tuple's elements. If not provided, an empty dbzero.tuple is created. |
<br/>
The method requires that the argument, if provided, is an iterable. Passing non-iterable types (e.g., int, None) or more than one argument will raise a TypeError.
Returns
A dbzero.types.Tuple object. This object supports standard tuple operations, including indexing, iteration, unpacking, comparison, and methods like count() and index().
Side Effects
- Reference Management: A
dbzero.tupleholds strong references to anydbzeroobjects it contains. If an object that contains adbzero.tupleis deleted withdbzero.delete(), thedbzeroobjects inside that tuple will also be deleted from the database upondbzero.commit().
Examples
Basic Creation and Usage
You can create a dbzero.tuple from any Python iterable. If you call it with no arguments, you get an empty tuple.
# Create an empty tuple
empty_tuple = db0.tuple()
print(len(empty_tuple))
# Expected output: 0
# Create a tuple from a list
# It can store various data types, including bytes
my_tuple = db0.tuple([1, "hello", b"world", True])
print(my_tuple[1])
# Expected output: "hello"
# Create from a generator expression
gen_tuple = db0.tuple(i for i in range(3))
print(gen_tuple == (0, 1, 2))
# Expected output: TrueStandard Tuple Operations
A dbzero.tuple behaves just like a regular Python tuple. You can iterate over it, unpack it, and use its methods.
data_tuple = db0.tuple(['apple', 'banana', 'cherry', 'banana'])
# Unpacking
a, b, c, d = data_tuple
print(b)
# Expected output: 'banana'
# Methods: count() and index()
print(f"Count of 'banana': {data_tuple.count('banana')}")
# Expected output: Count of 'banana': 2
print(f"Index of 'cherry': {data_tuple.index('cherry')}")
# Expected output: Index of 'cherry': 2
# Comparison with a standard Python tuple
assert data_tuple == ('apple', 'banana', 'cherry', 'banana')