rawData = yaml.safe_load(f)
reads the file, and that means the later yaml.load(f)
can't read any more data from the file. While you could rewind the seek pointer, there is absolutely no reason to: You've already loaded the YAML document (in a manner that is safer, too). Just do testObj.__dict__ = rawData
.
That said, I have reservations about assigning to __dict__
. It may or may not be implementation-defined, and in any case it reeks of a hack. There's zero validation, invalid data leads to a type error or attribute error (or with YAML.load
instead of safe_load
, even arbitrary other errors, including silent security breaches) later on in the program with no indication that the YAML file is at fault. A proper serialization library is a more robust and maintainable choice in the long run.