26

Is there any way to access the denominator in a fraction in Python 2?

For example, I have a variable:

x = fractions.Fraction(4,1)

And I want to access its denominator. How do I do that?

James Dunn
  • 8,064
  • 13
  • 53
  • 87
nsane
  • 1,715
  • 4
  • 21
  • 31
  • 12
    I was going to give you a hard time about not reading [the official documentation](http://docs.python.org/2/library/fractions.html), but I was appalled to find out that the documentation does not mention how to get the denominator (or numerator, for that matter)! It seems like a **huge** oversight. If a python developer happens by this page, please note that this really needs to be added to the `fractions` documentation. – SethMMorton Nov 03 '13 at 06:54
  • 3
    FYI, I did read the `fractions` [documentation](http://docs.python.org/2/library/fractions.html) before asking here. – nsane Nov 03 '13 at 06:56
  • Yes, as I pointed out in my comment the documentation is not helpful (it usually is very helpful, that's why I'm so surprised). I made that comment to point that out to others so they would not assume you didn't read the docs (as I had initially assumed till I saw the docs were not helpful). – SethMMorton Nov 03 '13 at 06:59
  • 4
    [A link to the fractions.py source code](http://hg.python.org/cpython/file/2.7/Lib/fractions.py) is given in the `fractions` documentation, but you would have to understand what the `@property` decorator does (and how classes are written) to have figured out how to access the denominator. *Technically* the docs author could argue that the information is there, but for a user new to the language it is **a)** not obvious to look there and **b)** difficult (or impossible if the user is new enough) to understand so it is still of no help. – SethMMorton Nov 03 '13 at 07:06
  • 1
    It looks like they finally added `numerator` and `denominator` to the [python 3.3 documentation](http://docs.python.org/3.3/library/fractions.html). I doubt it will be back-ported to python 2.7, though. – SethMMorton Jan 11 '14 at 19:08

1 Answers1

27
>>> from fractions import Fraction
>>> a = Fraction(1,2)
>>> a.denominator
2

Additionally Python's help() method can be very useful to determine exactly what methods and properties exist for an object. In the example above you could get a help description for the Fraction object by help(Fraction) or help(a) in an interpreter session.

  • Oh, and I was trying a.denominator(). Did not realise the parentheses would not come. Thanks! – nsane Nov 03 '13 at 06:31
  • 1
    The denominator and numerator are properties of the `Fraction` object and not methods. –  Nov 03 '13 at 06:33
  • 3
    @nisargshah95 I'm sure it was giving a message like "int object is not callable". That means that python thought you were trying to call an int like a function, i.e. adding the parentheses. When you get that message, it means you are adding parentheses when you aren't supposed to. – SethMMorton Nov 03 '13 at 06:37
  • Yes I did get `int object not callable` error. Thanks a lot for the clarification. – nsane Nov 03 '13 at 06:54