Issue
I come from Java, and I want to do some data transfer objects (DTOs) like this:
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?
Or is there a better way to do this?
Solution
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?
I am not sure what you are trying to say in this comment, but if I understand right, the best way to get something close is to define a method to add an error.
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))
Answered By - ddaa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.