docs(dsa): improve ListNode debugging implementation
- Replace `any` with `typing.Any` - Add return type annotation to `__repr__` - Include runtime value type in debug representation
This commit is contained in:
1 parent
ac46855845
commit
74f0b89523
1 file changed
+5
-3
@@ -128,17 +128,19 @@ This `ListNode` is the building block of your linked list, it's the minimal impl
|
|||||||
While learning the data structure, it is useful to enrich the class with debugging helpers. These additions are not part of the data structure itself; they simply make it easier to visualize the chain of nodes in memory.
|
While learning the data structure, it is useful to enrich the class with debugging helpers. These additions are not part of the data structure itself; they simply make it easier to visualize the chain of nodes in memory.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from typing import Any
|
||||||
class ListNode:
|
class ListNode:
|
||||||
def __init__(self, val: any = 0, next: 'ListNode | None' = None):
|
def __init__(self, val: Any = 0, next: 'ListNode | None' = None):
|
||||||
if next is not None and not isinstance(next, ListNode):
|
if next is not None and not isinstance(next, ListNode):
|
||||||
raise TypeError("`next` must be a ListNode or None.")
|
raise TypeError("`next` must be a ListNode or None.")
|
||||||
|
|
||||||
self.val = val
|
self.val = val
|
||||||
self.next = next
|
self.next = next
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self) -> str:
|
||||||
next_id = hex(id(self.next)) if self.next else None
|
next_id = hex(id(self.next)) if self.next else None
|
||||||
return f"ListNode(id={hex(id(self))}, val='{self.val}', next={next_id})"
|
return f"ListNode(id={hex(id(self))}, val={self.val}: {type(self.val)}, next={next_id})"
|
||||||
|
|
||||||
|
|
||||||
def inspect(self):
|
def inspect(self):
|
||||||
current = self
|
current = self
|
||||||
|
|||||||
Reference in new issue
Block a user