python - Python 3 打字注解和父子设计
问题描述
我正在编写一些 Python 代码,我必须使用这样的父子设计:
from typing import List
class Parent(object):
def add_children(self, child: List[Child]):
"""Do something here"""
class Child(object):
def set_parent(self, parent: Parent):
"""Do something here"""
但是 Python 提出了 aNameError
并抱怨Child
该类没有定义。这是合乎逻辑的,因为它在Parent
类之下。
C++ 中是否有类似“前向声明”的东西来处理这样的问题,还是有其他方法?我试图谷歌它没有成功。
解决方案
推荐阅读
- java - 如何在 jsoup 中找到带有伪元素 ::before 的 HTML 标记
- google-chrome-extension - 仅验证来自键盘的输入而不是来自自动填充
- java - 保存和检索 ArrayList
以共享偏好的有序方式 - azure - Azure 数据工厂查询
- javascript - 如何获得一个自定义钩子以首先响应加载,然后使用 setInterval 每 4 秒运行一次?
- python - 如何使用python组合df行?
- visual-studio - 每行执行两次
- java - 如何在 Spring Boot 中从端点响应中全局省略空 xml 标记?
- reactjs - Jest 测试回调
- mysql - SQL 关系除法查询