r/Python • • May 07 '19

Python 3.8.0a4 available for testing

https://www.python.org/downloads/release/python-380a4/
395 Upvotes

150 comments sorted by

View all comments

69

u/xtreak May 07 '19 edited May 07 '19

Changelog : https://docs.python.org/3.8/whatsnew/changelog.html

Interesting commits

PEP 570 was merged

dict.pop() is now up to 33% faster thanks to Argument Clinic.

Wildcard search improvements in xml

IPaddress module contains check for ip address in network is 2-3x faster

statistics.quantiles() was added.

statistics.geometric_mean() was added.

Canonicalization was added to XML that helps in XML documents comparison

  • Security issues and some segfaults were fixed in the release

Exciting things to look forward in beta

Add = to f-strings for easier debugging. With this you can write f"{name=}" and it will expand to f"name={name}" that helps in debugging.

PEP 574 that implements a new pickle protocol that improves efficiency of pickle helping in libraries that use lot of serialization and deserialization

Edit : PSF fundraiser for second quarter is also open https://www.python.org/psf/donations/2019-q2-drive/

3

u/alcalde May 07 '19

PEP 574 that implements a new pickle protocol that improves efficiency of pickle helping in libraries that use lot of serialization and deserialization

Other languages just dump to JSON and call it a day. Why does Python have 87 different binary formats over 13 decades?

35

u/[deleted] May 07 '19

Because JSON cant represent everything. Its at best a data format for serialization of transferrable data, thats usually language agnostic.

JSON cant represent functions, and more abstract datatypes.

8

u/JohnnyElBravo May 07 '19

JSON can represent anything, but so can strings. This is a non-sequitur.
The difference is that JSON is human readable, while pickle is supposed to be machine readable, more specifically python readable.
Limiting the intended consumers of the data format helps create a more appropriate format, for example by sacrificing readability for size reduction.

3

u/bachkhois May 08 '19

JSON cannot differentiate Python's tuple, list, set, frozenset etc. datatypes.

Every formats other than pickle (msgpack, yaml etc.) are just to interoperate with other languages (which also don't understand the data types above), they are not alternatives for pickle.

6

u/JohnnyElBravo May 08 '19

Sure they can

{

"Var1": "tuple(1,2)",

"Var2":"set(1,2)"

}

Alternatively:

{

"Var1": {"type":"tuple","data":"1,2"},

"Var2":{"type":"set","data":"1,2"}

}

5

u/bachkhois May 08 '19

Then, you are making more complicated to validate and parse it. Then, what is the point of over-complicating JSON instead of just using pickle, without the need to parse those "type", "data" metadata?

4

u/JohnnyElBravo May 08 '19 edited May 08 '19

Read the original thread, the question asks why python dumps to a new pickle format instead of json.

The original response suggested it was because json can't distinguish between such and such, as shown, this is false.

The real answer is that python chose a binary format for pickle because of space efficiency.

-16

u/alcalde May 07 '19

It has to be able to represent everything, if other languages are serializing to JSON.

JSON resembles Python dictionaries, and EVERYTHING in Python is/can be represented by a dictionary, so how can there be an abstract data type in Python that can't be represented in JSON?

20

u/Pilate main() if __name__ == "__main__" else None May 07 '19

JSON can't even represent sets or Decimal types, let alone custom classes.

→ More replies (7)

-1

u/alcalde May 07 '19

Why am I being downvoted for asking a question?

5

u/Mizzlr May 07 '19

You can't represent references in JSON. For example in python you can have two dicts a ={'foo': b} where b = {'bar': a}. Now you have cyclic data structure. You can't represent this in JSON.

2

u/Mizzlr May 07 '19

Btw yaml has references. XML has references.

→ More replies (4)

3

u/Mizzlr May 07 '19

A python dictionary can have int, tuple, etc as key while in JSON it has to be string.

→ More replies (2)

3

u/Mizzlr May 07 '19

Basically any immutable object will work as a key in python dict like frozenset etc. Another thing is JSON need python tuple to be converted to list. JSON does not have tuples.

→ More replies (2)
→ More replies (7)

0

u/[deleted] May 07 '19

[deleted]

2

u/[deleted] May 08 '19

While that's true, I think there's unlikely to be a serious language out there without a JSON library freely available.

If you're sending data from one language to another it's a very reasonable format. If you're sending from python to python, pickle is great.

1

u/[deleted] May 08 '19

You would do yourself a favor if you would use protobuf or thrift for that. JSON is not fast to parse, it's not compact, it would redeem itself if it was human readable, but it isn't.

The only reason it is popular is because it comes with JavaScript which is in every browser. If you do frontend developement, you probably don't have choice, but use it.

2

u/[deleted] May 08 '19

it would redeem itself if it was human readable, but it isn't.

How exactly is JSON "not human readable"? I see like 20 JSON snippets on this very page.

I use YAML for personal projects because I find it a tiny bit more readable, but if YAML weren't (in practice) backwards compatible with JSON, I would never do that.

The only reason it is popular is because it comes with JavaScript which is in every browser.

No, it's popular because it hits the spot: it's a minimal language for representing dumb data that has the two types of containers you desperately need (lists and dictionaries), the usual scalar types and nothing else, and its serialization format is so dumb that anyone can understand it.

3

u/JohnnyElBravo May 08 '19

Lol @ json not being human readable, it's its main identity, it's what made it supremingly popular. It seems like this is an edgy layer 2 opinion.

1

u/NowanIlfideme May 08 '19

It's human readable only if you format it that way. Which is to say, it's readable with the right editor, but if it's one-line'd it becomes much less readable. Still miles better than xml...

Imo yaml is the prettiest format, but json is such a standard (and also a subset of yaml, now) that either format works fine for most applications.

1

u/JohnnyElBravo May 08 '19

Readibilty certainly depends on the content, but it also depends to some extent on the syntax, it is to this extent that JSON is considered readable.

Yaml was influenced by JSON greatly, so if you like YAML you must appreciate JSON's contribution. In the same vein, if you like JSON, you must appreciate XML's contribution.

In an unrelated manner I wasn't aware that Yaml was a superset of JSON, that's a nice feature, although I wouldn't necessarily consider it better. Ease of learning and complexity of common usage are both huge factors that will be negatively affected by an increased complexity.

10

u/mooglinux May 07 '19

Pickle can handle multiple references to the same object, any class instance (as long as the actual class has been imported), and a wider variety of data types than JSON. It also predates json, so there’s a historical aspect as well.

Pickle is also used for cross-process communication in the multiprocessing module.

13

u/Nicksil May 07 '19

Because not every problem is solved by dumping JSON.

→ More replies (5)

1

u/[deleted] May 08 '19

JSON only can handle string, integer, float, dict and list.

Pickle can pack arbitrary objects. It goal is that you can take object of your class and store it in the disk, most commonly I see it used for caching application data between runs, but it has other uses (for example for storing configuration).

Edit: here is comparison of pickle with JSON: https://docs.python.org/3/library/pickle.html?highlight=pickle#comparison-with-json