1

I want to have a string representation of a python object. I used to do it with the famous pickle, but I'm wondering if it exists something better and less expensive. I don't like to use repr and eval cause they are not secure.

1
  • 2
    Pickle isn't secure either, look at the warning right at the top of the pickle docs Commented Apr 6, 2012 at 21:03

3 Answers 3

1

YAML is a fairly decent format for serializing datastructures. It's like JSON, but more so. The PyYAML library works fairly well. In addition to letting it guess how to serialize datastructures, you can get fairly specific. See their full documentation for examples.

Sign up to request clarification or add additional context in comments.

2 Comments

YAML or JSON would be a great pick.
The nice thing about JSON, in addition to being in the standard library, is that it's a subset of YAML. So if you start with JSON and move to YAML later, the YAML parser will still be able to read the data. YAML generally looks nicer, though, and has some features that JSON lacks. I usually make my object dumpers fallback to JSON when possible if import yaml fails.
1

Use repr() to get the string representation, and ast.literal_eval() to recover the object.

Or just use pickle. If you are using Python 2.x you can import cPickle to get a faster pickle, but in Python 3.x there is just the one pickle and it is the C one.

Personally, I like to use JSON for simple objects; you might try import json and see if it works for you.

3 Comments

ast.literal_eval() and eval() are not exact science. Used to fail with complex objects.
ast.literal_eval() will not reconstruct an object from its repr() except in a few limited cases. In fact, in the general case where __repr__ is not overloaded, and object cannot be reconstructed from its repr(), because that just looks like <module.name_of_class instance at 0xdeadbeef>.
Even if you have, say, an immutable class that can always infer what arguments it was constructed with, and overload __repr__ to produce a string that looks like a constructor call, ast.literal_eval() still won't handle it because that's an expression, not a literal. You'd need to move up to eval, which is insecure.
0

By object representation, if you mean serialization then pickle is an option. If you want a faster implementation try cPickle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.