Skip to content

Improve __repr__ and __str__ methods of WordMorphism #13677

@seblabbe

Description

@seblabbe

The __repr__ method is not fun:

sage: WordMorphism('a->ab,b->ba')
Morphism from Words over Ordered Alphabet ['a', 'b'] to Words over Ordered Alphabet ['a', 'b']
sage: WordMorphism({0:[0,1],1:[1,0]})  
Morphism from Words over Ordered Alphabet [0, 1] to Words over Ordered Alphabet [0, 1]

One always have to use the print statement (which calls __str__) ::

sage: print WordMorphism('a->ab,b->ba')
WordMorphism: a->ab, b->ba
sage: print WordMorphism({0:[0,1],1:[1,0]})
WordMorphism: 0->01, 1->10

Also, this is ugly:

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]})
sage: E = E1Star(sigma)                              
sage: E                                              
E_1^*(WordMorphism: 1->12, 2->13, 3->1)

The proposed solution is to change the code of __repr__ so that it behaves like the actual __str__ and change the __str__ to remove the WordMorphism: part. See examples below :

The new __repr__ method :

sage: WordMorphism('a->ab,b->ba')
WordMorphism: a->ab, b->ba
sage: WordMorphism({0:[0,1],1:[1,0]})
WordMorphism: 0->01, 1->10

The new __str__ method :

sage: print WordMorphism('a->ab,b->ba')
a->ab, b->ba
sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]})
sage: E = E1Star(sigma)                              
sage: E  
E_1^*(1->12, 2->13, 3->1)

CC: @sagetrac-tjolivet @videlec

Component: combinatorics

Keywords: wordmorphism

Author: Sébastien Labbé

Reviewer: Timo Jolivet

Merged: sage-5.5.rc0

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions