Skip to content

libgap: distinct __str__ and __repr__ for GapElements #27946

@embray

Description

@embray

Currently the libgap.GapElement objects have a __repr__ (but no explicit __str__, so the two are the same) which is taken from calling the GAP function ViewObj on the object and capturing and returning the result. This is essentially the same as when you display the object in a GAP interpreter prompt without using Print(), such as:

gap> a := "hello";
"hello"
gap> a;
"hello"

Although GAP has many options for displaying/printing variables, this makes ViewObj the closest possible thing to the __repr__ method on Python objects, which is called in similar circumstances:

>>> a = "hello"
>>> a
"hello"

This is in stark contrast with the pexpect interface's GapElement which (through a sequence of calls) effectively implements its __repr__ by calling Print(obj). Arguably this was not the best choice, but changing it might be tricky at this point. However, it does not explicitly implement __str__, so GapElement.__str__ does give a sensible result in this case.

Arguably Print(obj) is the right choice for __str__ since again in rough comparison with Python, when you print(...) an object in Python its __str__ is called.

This proposes to change just the libgap interface to meet this specification: libgap.GapElement.__str__ captures the output of Print(obj) and returns it. While libgap.GapElement.__repr__ captures and returns the output of ViewObj(obj).

This gives the best of both worlds, and better consistency in most contexts where GapElements are converted to strings, making replacing pexpect gap with libgap easier and less disruptive.

CC: @nthiery

Component: interfaces

Keywords: gap libgap

Author: Erik Bray

Branch/Commit: 63db722

Reviewer: Vincent Delecroix

Issue created by migration from https://trac.sagemath.org/ticket/27946

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions