self.surname = surname

def yaml(self):
    return yaml.dump(self.__dict__)

@staticmethod
def load(data):
    values = yaml.safe_load(data)
return User(values['name'], values['surname'])
user = User('spam', 'eggs')
serialized_user = user.yaml()
print('serialized_user:  %s' % serialized_user.strip())
deserialized_user = User.load(serialized_user)
print('name: %s, sname: %s' % (deserialized_user.name, deserialized_user.
    surname))
How to deserialize an object with PyYAML using safe_load?