gpt4 book ai didi

python - 定义一个带有类型的列表

转载 作者:行者123 更新时间:2023-11-28 22:07:36 28 4
gpt4 key购买 nike

我来自 Java,我想做一些事情 data transfer objects (DTO) 像这样:

class ErrorDefinition():
code = ''
message = ''
exception = ''

class ResponseDTO():
sucess = True
errors = list() # How do I say it that it is directly of the ErrorDefinition() type, to not import it every time that I'm going to append an error definition?

或者有更好的方法吗?

最佳答案

你可以输入一个列表作为python 3.8.4

https://docs.python.org/3/library/typing.html

from typing import List
vector:List[float] = list()

from typing import List
vector:List[float] = []

然而 Python 是动态类型的 https://www.python.org/dev/peps/pep-0484/打字使代码更容易阅读、更容易理解、更有效地使用您的 IDE 工具并创建更高质量的软件。我想这就是它出现在 PEP 中的原因。

关于python - 定义一个带有类型的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1924469/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com