我来自Java,我想做一些像这样的数据传输对象(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?
Run Code Online (Sandbox Code Playgroud)
或者有更好的方法吗?
Sha*_*yan 11
您可以将列表输入为 python 3.8.4
https://docs.python.org/3/library/typing.html
from typing import List
Vector = List[float]
Run Code Online (Sandbox Code Playgroud)
Python 是动态输入的,但是https://www.python.org/dev/peps/pep-0484/ 输入可以更轻松地阅读代码,更容易理解它,有效地使用您的 IDE 工具并创建更高质量的软件。我想这就是它在 PEP 中的原因。
error = list() # 我该如何说它是直接的 ErrorDefinition() 类型,而不是每次我要附加错误定义时都导入它?
我不确定你想在这个评论中说什么,但如果我理解正确的话,接近的最好方法是定义一个方法来添加错误。
class ResponseDTO(object): # New style classes are just better, use them.
def __init__(self):
self.success = True # That's the idiomatic way to define an instance member.
self.errors = [] # Empty list literal, equivalent to list() and more idiomatic.
def append_error(self, code, message, exception):
self.success = False
self.errors.append(ErrorDefinition(code, message, exception))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26319 次 |
| 最近记录: |