11.09.20 23:28, The Nomadic Coder пише:
Hi All,
This is the first time I'm posting to this mailing group, so forgive me if I'm making any mistakes.
So one of the most common ways to load json, is via a file. This is used extensively in data science and the lines. We often write something like :-
with open(filename.json, "r") as f: my_dict = json.load(f)
or my_dict = json.load(open("filename.json", "r"))
Since this is sooooo common, why doesn't python have something like :- json.loadf("filename.json")
Is there an obvious issue by defining this in the cpython? I don't whipping up a PR if it gains traction.
Similar ideas were already proposed and rejected multiple times. First, there is a principle "not every three-line function needs to be a built-in". Every new function increases maintenance cost and cognitive burden. You can think that it is small, but there are thousands of such "useful" helpers. It is easier to learn how to combine simple builtin blocks than remember names and arguments for all combinations. Second, the resulting function would have monstrous interface. open() takes 8 arguments, and json.load() can take at least 8 arguments, and they should be combined. And thank that open() does not accept arbitrary var-keyword arguments as json.load(), resolving this conflict would be impossible. Third, this combination is not so common as you think. On my current work JSON is used everywhere, but in most cases it is received from internet or load from database. If loading from a file is the most common ways to load JSON in your program, it is not hard to write your own helper with interface and defaults that suit you. It will take less time than writing a letter in a mailing list.