1

Python fractions library enables a user to declare a fraction, like this:

f = Fraction(3, 15)

The problem is, I want to keep track of the numerators and denominators, whereas the example above is equivalent to:

f = Fraction(1, 5)

Is there a way to do this without writing my own class?

shooqie
  • 950
  • 7
  • 17
  • [This](http://stackoverflow.com/q/17537613/1377864) might be useful. – Yaroslav Admin Feb 01 '16 at 13:32
  • 3
    You can probably write a lightweight subclass that saves a multiplier you want applied in `__repr__`. Or how else is `Fraction(3, 15)` supposed to behave differently than `Fraction(1, 5)`? – L3viathan Feb 01 '16 at 13:33
  • 2
    No, there isn't a sane way. Write your own class. – Mark Dickinson Feb 01 '16 at 14:53
  • Could you maybe explain what you're trying to achieve? Why do you need to keep track of the numerators and denominators? What's this for? – Mark Dickinson Feb 02 '16 at 09:30
  • A task. Basically you have nominators in denominators in a `txt` file and one of the subtasks is to find how many of the fractions are truncated, among with couple for other things for which I need to keep track of numerators and denominators. I figured I'd be better off using an existing class instead of writing my own. One of the subtask is to find the smallest fraction, and I need to give the fraction given in the file, not a truncated one (if it is truncatable). – shooqie Feb 02 '16 at 11:23
  • 1
    Then there's no need for a custom class at all: you could read the file into a list of pairs of the form `(original_line_from_file, Fraction_object)`, or into triples of the form `(numerator, denominator, Fraction_object)`, for example. Then it's easy to sort on the fraction representation but output the corresponding line from the file. – Mark Dickinson Feb 02 '16 at 20:03
  • That's clever actually, thanks! I asked this question out of curiosity though; in the end I'll have to write it in Java, which means writing my own class is inevitable. – shooqie Feb 04 '16 at 07:17

0 Answers0