3 ms·
Because it monkey patches the way imports work. The context manager isn't bad really, although there are probably more natural ways to create a python object wi
by ngoldbaum 11y ago
Because it monkey patches the way imports work. The context manager isn't bad really, although there are probably more natural ways to create a python object with the same structure as a JSON file without abusing the import system.
- pmiller2 11y agoRight. JSON is almost valid Python to begin with. Off the top of my head, the main differences are that JSON true needs to be True in Python (likewise with false/False, null/None). It's not that difficult.
- pseudonym 11y agoIt's also slightly stricter with syntax; you can't have extra ending commas. {'foo' : 1, 'bar' : 2,} is valid Python but not valid json.
- osullivj 11y agoYes, I keep running into this. I leave trailing commas in my JSON as it evals fine in Python. Then my C++ code, using boost::property_tree chokes on it. property_tree also needs double quotes, not the single quotes in this example.
- to3m 11y agoThere's a module for this: json. The error messages might not be very good, but it does check for trailing commas.
- nothrabannosir 11y agoTo note: Python 3, due to the whole string vs unicode literals thing (\u)