I am trying to convert data from a simple object graph to a dictionary. I do not need type information or methods and I do not have to be able to transform it back into an object again.
I found, but it does not repeat.
Being relatively new in Python, I'm worried that my solution can be ugly, or may be unpredictable, or broken into something vaguely, or just plain old NIH.
My first attempt continued to work until I tried to do it with lists and dictionaries, and it just seemed easy to see that the object got an internal dictionary, and If not, to treat it only as a value (instead of checking it all). My previous attempts also did not amend the list of objects:
def todict (obj): if heetter (obj, "__iter__"): back to obj [todict (v)] alif Return to the key (dict (value,), obj .__ dict __. Iteritems () Value if not (value) and not key.startswith ('_')] Other: return Obj
This seems to work better and does not require exceptions, but I still can not believe that there is no information about the cases.
< / P>
The clues taken from my efforts and the answers from Anurag Uiaial and Lannant Regebro, my best work for me Are:
Comments
Post a Comment