[英]Python class attribute can't use its own object
我是 python 的新手,我編寫了兩個類來定義一個單鏈表 object。 我的 _Node class 有問題。 對於 _Node class 的“下一個”屬性,我想將其類型設置為 _Node object 或 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
您需要在文件頂部進行以下導入以啟用對尚未定義的類的引用
from __future__ import annotations
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.