python - How do I write a Python class that is a node in a linked list while using type hinting
问题描述
In Python 3.7, I have a dataclass that is a node in a linked list.
from dataclasses import dataclass
@dataclass
class Node(object):
right: Node
left: Node
The problem is that I get an inspection error in Pycharm:
Unresolved reference 'Node'
and this error when running
NameError: name 'Node' is not defined
dataclass requires I declare the type, so how do I solve this?
解决方案
PEP 563 solves this.
By adding the following import, the type hinting is resolved later on.
from __future__ import annotations
推荐阅读
- flutter - 哪个 API 用于在 facebook 中创建宣传?
- r - R:从平均值中查找不包括该行值的列的平均值
- android - 使用 FlatList 裁剪图像
- html - 带有右对齐项目的引导导航栏
- python - 在 cnn 和 unet 中显示权重和激活
- ios - 使用 @objc 从 Objective-C 访问 Swift 扩展中的可选变量
- android - 调度不在几个小时范围内的通知时,颤振本地通知包不起作用
- python - for循环中的数组列表索引超出范围
- asciidoc - Spring Auto Rest Docs 记录了所有 Json 子类型
- azure-virtual-machine - VM 规模集不允许将诊断设置发送到 Log Analytics