[Python-ideas] `to_file()` method for strings

Chris Angelico rosuav at gmail.com
Fri Mar 25 17:58:42 EDT 2016


On Sat, Mar 26, 2016 at 3:30 AM, Franklin? Lee
<leewangzhong+python at gmail.com> wrote:
> I was going to suggest that, if str save/load were to be implemented, it
> could be made as a parallel to json.dump and json.load (as string module
> members?). Any new features like atomicity would be "backported" to
> json.dump/load. Same with pickle.load/dump. It's just str serialization,
> isn't it?
>
> If there is a save/load builtin (or dump/load), I don't see what's so
> special about JSON. (YAML is more like Python, including in readability.)
> I'd suggest that you can specify a "format" parameter (or with another name)
> to use it with pickle, json, yaml, etc., but that means two ways of doing
> things. Deprecate the original way?
>
>     import json
>     load("data.json", format=json)

The thing that's special about JSON is that nearly everyone recognizes
the name. Thanks to JSON's extensive use in networked systems
(courtesy of web browser <-> web server communication), pretty much
everyone will be able to grok bidirectional JSON handling. Maybe YAML
would be better, but if you publish something that uses JSON, nobody
will be confused. Not sure that's enough justification to make it the
default, but it is significant.

> Without a format kwarg, Python might look at the file extension and try to
> guess the format. Modules could register extensions (is this magic?), or it
> could look for an object with the same name which has __dump__/__load__ (is
> this VERY magic?).
>
> I don't like either: You have to import pickle to load it, but you wouldn't
> have to name it explicitly to use it. I'd hate `import pickle;
> pickle.register(".p")`: this thread is for scripters and not application
> devs, so wordiness matters.

You can slap those lines into sitecustomize.py though. Or third party
Python distributors could slap those lines into sitecustomize.py, and
you won't even be aware that stuff had to be registered. But I don't
like the registration of extensions as a means of selecting an output
format, generally. Too magical.

ChrisA


More information about the Python-ideas mailing list