Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Created September 10, 2010 20:11
Show Gist options
  • Save mccutchen/574291 to your computer and use it in GitHub Desktop.
Save mccutchen/574291 to your computer and use it in GitHub Desktop.
# Answering this question:
# http://stackoverflow.com/questions/3679306/
>>> import json
>>> class Foo(object):
... def __init__(self, a, b):
... self.a = a
... self.b = b
...
>>> foo = Foo('a string', ['a', 'list', 'of', 'strings'])
>>> foo
<__main__.Foo object at 0x1006ae190>
>>> json.dumps(foo.__dict__)
'{"a": "a string", "b": ["a", "list", "of", "strings"]}'
>>> json.loads(_)
{'a': 'a string', 'b': ['a', 'list', 'of', 'strings']}
>>> foo2 = Foo(**_)
>>> foo2
<__main__.Foo object at 0x1006ae0d0>
>>> foo2.__dict__
{'a': 'a string', 'b': ['a', 'list', 'of', 'strings']}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment