Py学习  »  Python

避免类型化python中的顺序依赖

Phil Lord • 5 年前 • 1489 次点击  

我在玩输入的python。它基本上运行良好,但我有 因为顺序依赖性而重新排序我的很多代码。例如 考虑一下这个玩具代码:

from typing import ClassVar, List

class MyFirstClass:
    attr: MyClass

class MyClass:
    # You can optionally declare instance variables in the class body
    attr: int

class MySecondClass:
    attr: MyClass

这无法正确编译,因为 MyClass 未声明 在我的头等舱里:

Traceback (most recent call last):
  File "Temp.py", line 5, in <module>
    class MyFirstClass:
  File "Temp.py", line 6, in MyFirstClass
    attr: MyClass
NameError: name 'MyClass' is not defined

惹人生气的。这附近有吗?我做错什么了吗? 我已经想出了最好的办法来定义我的类,首先 时间没有躯体。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39745
 
1489 次点击  
文章 [ 1 ]  |  最新文章 5 年前