Python class 属性不能使用它自己的对象
Python class attribute can't use its own object
我是 python 的新手,正在编写两个 classes 来定义单链表对象。我的 _Node class 有问题。对于 _Node class 的 'next' 属性,我想将其类型设置为 _Node 对象或 None,这就是我写 Optional[_Node] 的地方。但是 Pycharm IDE 不识别它。有人可以帮我解决这个问题吗?非常感谢。
from typing import Any, Optional
class _Node:
""" A node in a linked list.
Note that this is considered a "private class", one which is only meant
to be used in this module by the LinkedList class, but not by client
code.
=== Attributes ===
item:
The data stored in this node.
next:
The next node in the List, or None if there are no more nodes.
"""
item: Any
next: Optional[_Node]
def __init__(self, item: Any):
""" Initialize a new node storing <item>, with no next node.
"""
self.item = item
self.next = None # Initailly pointing to nothing
您需要在文件顶部进行以下导入,以启用对具有 not yet been defined
的 类 的引用
from __future__ import annotations
我是 python 的新手,正在编写两个 classes 来定义单链表对象。我的 _Node class 有问题。对于 _Node class 的 'next' 属性,我想将其类型设置为 _Node 对象或 None,这就是我写 Optional[_Node] 的地方。但是 Pycharm IDE 不识别它。有人可以帮我解决这个问题吗?非常感谢。
from typing import Any, Optional
class _Node:
""" A node in a linked list.
Note that this is considered a "private class", one which is only meant
to be used in this module by the LinkedList class, but not by client
code.
=== Attributes ===
item:
The data stored in this node.
next:
The next node in the List, or None if there are no more nodes.
"""
item: Any
next: Optional[_Node]
def __init__(self, item: Any):
""" Initialize a new node storing <item>, with no next node.
"""
self.item = item
self.next = None # Initailly pointing to nothing
您需要在文件顶部进行以下导入,以启用对具有 not yet been defined
的 类 的引用from __future__ import annotations