2

I have my custom class that represents an object. I want to make that object compatible with "ast.literal_eval()"

How can I do that? I can add necessary method/code to my class if necessary.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
leopoodle
  • 2,110
  • 7
  • 24
  • 36
  • 4
    `ast.literal_eval` evaluates literals. Python does not support user defined literals. Ergo, you can't use `ast.literal_eval` to produce a user defined type. If you could, all the security guarantees of `ast.literal_eval` would go out the window. – ShadowRanger Mar 18 '17 at 05:39

2 Answers2

4

You cannot. You will need to reimplement it from scratch, adding support for your class within it.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Because you tagged your question with Pyro, I assume you're asking it in the context of serializing your own classes when using Pyro's remote method calls. Pyro provides a mechanism to do that.

Irmen de Jong
  • 2,739
  • 1
  • 14
  • 26