Archived

This forum has been archived. Please start a new discussion on GitHub.

Python: repr() of Slice structs/classes

Consider this Slice definition:
struct Employee {
  long number;
  string firstName;
  string lastName;
};

In Python, the repr() of an object of this type yields
{
    number = 42
    firstName = Christian
    lastName = Bauer
}
which looks nice (apart from the fact that the structure type name is not included) but can't be fed into eval() as it should be. I would expect something like
Employee(42, 'Christian', 'Bauer')
or
Demo.Employee(number=42, firstName='Christian', lastName='Bauer')

Same for Slice classes.

Comments

  • mes
    mes California
    Hi Christian,

    Welcome to the forum.

    Technically you're right, the Python docs indicate that repr should return a valid Python expression "if at all possible". We currently do not follow this guideline because of the complexity that would be involved in handling object graphs, and in particular circular references.

    Is the current Ice behavior having an adverse impact on your application?

    Best regards,
    Mark
  • mes wrote: »
    Is the current Ice behavior having an adverse impact on your application?

    We do not depend on repr()-eval()-consistency, but having classes show up in the same way as they are input is less confusing for users writing scripts who know about Python but not about Slice (they're not supposed to). Plus, it's super-convenient to be able to copy-and-paste output to input in an interactive Python session. :D

    My current workaround is to overwrite the __repr__() methods of the (relevant) classes in a wrapper module.