list_node.py
class ListNode:
def __init__(self, val: int, next: ListNode):
self.val = val
self.next = next
When I wrote a type annotation like the above (one-way linked list constructor), a warning was displayed if ListNode was not defined.
undefined name 'ListNode'
from __future__ import annotations
list_node.py
from __future__ import annotations
class ListNode:
def __init__(self, val: int, next: ListNode):
self.val = val
self.next = next
assert ListNode.add.__annotations__ == {
'next': 'ListNode'
}
Since from __future__ import annotations
cannot be used in Python 3.7 or less, use a string.
list_node.py
class ListNode:
def __init__(self, val: int, next: 'ListNode'):
self.val = val
self.next = next
Recommended Posts