我来自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?

还是有更好的办法?

最佳答案

errors=list()#我怎么说它直接属于error definition()类型,而不是每次追加错误定义时都导入它?
我不知道你在评论中想说什么,但如果我理解正确,最好的接近方法是定义一个方法来添加一个错误。

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))

10-06 05:17
查看更多