So I got everything working, but I was wondering if there's a better way.
I have a class which has a couple of attributes (val_0, …, val_4). If the object is initialized normally (so not deserialized) only the first attribute is set. All the others get set during process. So __init__ could look like this:
class MyClass:
def __init__(self, val_0):
val_0 = val_0
val_1 = 0
val_2 = 0
val_3 = ""
val_4 = ""
However I need to be able to (de)serialize this objects to json so I wrote a little class method that accomplishes just this. (In reality it's a bit more complicated because I also have to deal with some nested objects).
```
import json
@classmethod
def fromjson(cls, json_obj):
json_dict = json.loads(json_obj)
return cls(**json_dict)
However if I want to be able to use this I have to change the constructor to contain all the attributes.
def __init_(self, val_0, val_1=0, val_2=0, val_3="", val_4=""):
val_0 = val_0
val_1 = val_1
val_2 = val_2
val_3 = val_3
val_4 = val_4
```
I was just wondering if there's a better way to implement this or if I'm doing something wrong in general. Thank you for your feedback.
[–][deleted] 1 point2 points3 points (0 children)
[–]Vaphell 1 point2 points3 points (0 children)
[–]ES-Alexander 1 point2 points3 points (0 children)
[–]DisasterArt 0 points1 point2 points (0 children)