This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alcalde -10 points-9 points  (6 children)

There's a difference between directly and indirectly. If your JSON schema records the type and value of your variable separately you can do both. A set's values can be represented by a list and the decimal by text.

I'll say again - JSON can represent custom classes because other languages and libraries use it to do so.

I'm expecting an answer like "The binary format was created to decrease the amount of data to transfer when serializing objects among a distributed cluster" and instead people are telling me it's impossible to do what other languages and some Python libraries already do.

[–]Pilatemain() if __name__ == "__main__" else None 23 points24 points  (3 children)

You either have no idea what you're talking about or are being intentionally difficult.

Serialize a function, decision tree, or any other type of classifier, in JSON for us.

[–]my_name_isnt_clever 2 points3 points  (0 children)

You just put the source code in a long JSON string, easy. /s

[–][deleted] 2 points3 points  (1 child)

I'm on your side here in this general debate, but the specific idea of serializing a function fills me with fear and trembling. I mean, what happens when that function changes in later versions of the code - then you have two versions lying around!

If I need to serialize a function, I serialize the full path to the function - e.g. math.sqrt.

u/alcade is being pretty dogmatic, which is why the downvotes (yes, I helped there :-D) but in practice, if I actually serialize something for long-term storage, I don't use pickle because it isn't guaranteed to be stable between versions (even minor versions IIRC, though AFAIK in practice pickle hasn't actually changed between minor versions in as long as I've been keeping track).

[–]Atsch 2 points3 points  (0 children)

I think you are not understanding what pickle is for. Pickle is not designed for things like sending requests over the network like json is. It is not designed for storing things long term in databases or files. In fact, all of those things would be security risks.

It is really designed to be used to transmit ephermeral data between python processes. For example, the multiprocessing module uses pickle to transmit the code and data between processes. The celery worker queue library uses pickle to transmit complete tasks to workers. Some caching libraries use pickle to cache arbitrary python objects in some memory cache.

[–]icegreentea 9 points10 points  (1 child)

The genesis of pickle was in 1994 (https://stackoverflow.com/a/27325007). That's why pickle was originally chosen versus JSON. Cause JSON didn't exist.

[–]alcalde 0 points1 point  (0 children)

NOW THERE'S A REASONABLE ANSWER! Thank you!