From: Kirill S. <xi...@ga...> - 2008-12-07 11:05:04
|
Hi Fabrizio, > Using the python object constructor capability of YAML, I am trying to > saparate object attributes in those that are not configurable (set at > init time by the application code) and in those that are configurable > (loaded at init time by YAML). One possible solution is in the following > example. I dont like it very much, there is any better way to do it? > Thanks in advance for any help. PyYAML uses the pickle protocol (see PEP 307) to construct YAMLObject instances and thus it never calls the __init__ constructor. By default, PyYAML simply updates the __dict__ dictionary with the content of the YAML node. You may override this behavior by providing a __setstate__ method accepting a single argument 'state', which is the node content. For instance, if you add a __setstate__ method to your Test class, the object corresponding to the document: --- !Test yaml_attr: attribute from yaml will be constructed in the following way: obj = create a new Test object, but don't call Test.__init__() obj.__setstate__({'yaml_attr': 'attribute from yaml'}) return obj Thanks, Kirill |