I have two classes that are inherited from the parent.
+------------+
| Parent |
|------------|
|attribute |
+------------|
+------------+
^ ^
| +----------------+
| |
+-----+------+ +-----+------+
| SonSelf | | SonFromYaml|
|------------| |------------|
|attribute | | |
+------------| +------------|
+------------+ +------------+
class Parent:
def __init__(self):
self.attribute = None
The first class fills in its attributes (calculates them) and writes itself to the yaml file.
class SonSelf(Parent):
def __init__(self):
super().__init__()
self.attribute = 'not blank'
with open('file.yml', 'w') as f:
yaml.dump(self, f)
The second class reads these attributes from yaml.
class SonFromYaml(Parent):
def __init__(self):
super().__init__()
with open('file.yml', 'r') as f:
ret = yaml.load(f, Loader=yaml.Loader)
self = ret
But for some reason the created object turns out to be empty.
>>> son_self = SonSelf()
>>> son_from_yaml = SonFromYaml()
>>> print("son_self.attribute: ", son_self.attribute)
>>> print("son_from_yaml.attribute: ", son_from_yaml.attribute)
son_self.attribute: not blank
son_from_yaml.attribute: None
The ret
object itself is identical to the son_self
object. I can see this in debug.
Is it possible to do so:
self = ret
Or it is necessary to forcibly copy each field from the object obtained when reading the yaml?
class SonFromYaml(Parent):
def __init__(self):
super().__init__()
with open('file.yml', 'r') as f:
ret = yaml.load(f, Loader=yaml.Loader)
self.attribute = ret.attribute