Title: Change the fractions.Fraction class to convert to a unicode fraction string
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, gappleto97, mark.dickinson, r.david.murray, rhettinger, steven.daprano, tim.peters, vstinner
Priority: normal Keywords: patch

Created on 2018-05-01 22:39 by gappleto97, last changed 2018-05-02 16:46 by gappleto97. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6678 open gappleto97, 2018-05-01 22:40
Messages (10)
msg316026 - (view) Author: Gabe Appleton (gappleto97) * Date: 2018-05-01 22:39
Currently it has a __repr__() which returns `Fraction(x, y)`, and a __str__() which returns `x/y`. I have a ready pull request to change this to a scheme where both return unicode fractions.
msg316027 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-05-01 22:52
Your patch would break the usual and useful behavior of x == eval(repr(x))

>>> f = Fraction(1,2)
>>> repr(f)
'Fraction(1, 2)'
>>> eval(repr(f))
Fraction(1, 2)
>>> f == eval(repr(f))

Plus, I'm sure there's working code that would break as a result of this.

I'd suggest having a utility function that provides the functionality that you're after.
msg316028 - (view) Author: Gabe Appleton (gappleto97) * Date: 2018-05-01 22:54
Would it be workable if I instead just changed the __str__() method? I'm willing to go either way, but I feel like it's a bit nicer to have it output a nice fraction that way.
msg316029 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-05-01 23:06
I'd bring it up on python-ideas, and point the discussion to this issue. I think the primary complain will be using non-ASCII characters in a function that normally doesn't return non-ASCII. But maybe people will be willing to accept it.
msg316030 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-05-01 23:23
I vote -1.  It's cute, but I'd much rather have a consistently ascii representation of something that is easily represented in ascii.
msg316034 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-05-02 01:44
Strings in Python 3 are already unicode.

Looking at the patch, I see a lot of fractions which display as the missing glyph white square. For example, instead of seeing 1/9, which displays perfectly everywhere, I see a mysterious box similar to □.

Even when I see one, there's the visual inconsistency between fractions which display like ½ and those that display like ¹²/₄₅ which frankly just looks ugly to me. One problem is that in many fonts, the glyphs for superscript and subscript digits are a hodge-podge of sizes and styles with no consistent design.

If you're going to do this, you ought to use U+2044 FRACTION SLASH rather than U+002F SOLIDUS: compare ¹²⁄₄₅ with the above. If your font is decent, and many are not, the fraction slash is tighter and allows the numerator and denominator to overlap the slash, closer to the visual look of ½.

I would strongly oppose this becoming the default __str__ of fractions.
msg316050 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-05-02 07:17
-1 from me. Apart from anything else, the output for a general fraction looks (to my eyes) ugly in a fixed-width font: the tiny digits are harder to read, and there's too much space between successive numerator (or denominator) digits: those digits are designed for superscripts or subscripts.
msg316076 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-05-02 14:49
In the Monaco font, the superscripts ⁰¹²³⁴⁵⁶⁷⁸⁹ don't all line-up.  The 1, 2, and 3 are lower which makes the fraction look weird.
msg316077 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-05-02 14:54
I dislike using non-ASCII characters for the default implementation. I don't think that this formatting is popular, and I expect many troubles on each platform.

IMHO it's very easy to put such short function in your favorite module, or directly into your application, for your own usage. Call it my_nice_fraction_formatting() :-)
msg316080 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2018-05-02 16:08
-1.  We should stop pretending this _ might_ happen ;-)
Date User Action Args
2018-05-02 16:46:27gappleto97setstatus: open -> closed
resolution: rejected
stage: patch review -> resolved
2018-05-02 16:08:37tim.peterssetnosy: + tim.peters
messages: + msg316080
2018-05-02 14:54:28vstinnersetnosy: + vstinner
messages: + msg316077
2018-05-02 14:49:02rhettingersetnosy: + rhettinger
messages: + msg316076
2018-05-02 07:17:10mark.dickinsonsetnosy: + mark.dickinson
messages: + msg316050
2018-05-02 01:44:19steven.dapranosetnosy: + steven.daprano
messages: + msg316034
2018-05-01 23:23:36r.david.murraysetnosy: + r.david.murray
messages: + msg316030
2018-05-01 23:06:56eric.smithsetmessages: + msg316029
2018-05-01 22:54:16gappleto97setmessages: + msg316028
2018-05-01 22:52:33eric.smithsetnosy: + eric.smith
messages: + msg316027
2018-05-01 22:40:32gappleto97setkeywords: + patch
stage: patch review
pull_requests: + pull_request6372
2018-05-01 22:39:00gappleto97create