Search code examples
pythonintcode-documentation

python3, in help(int) text, what is the Data descriptors described?


When you get the help string for int types using help(int). The very last part is:

| ----------------------------------------------------------------------
|  Data descriptors defined here:
|  
|  denominator
|      the denominator of a rational number in lowest terms
|  
|  imag
|      the imaginary part of a complex number
|  
|  numerator
|      the numerator of a rational number in lowest terms
|  
|  real
|      the real part of a complex number

So these are attributes of the complex type and fraction class, so why are they listed here in relation to ints. Are the some type of global data descriptor?


Solution

  • The fact that complex and fractions.Fraction have attributes with those names and meanings doesn't mean ints don't also have such attributes. Different classes are free to have similar attributes:

    >>> (5).denominator
    1
    >>> (5).imag
    0
    >>> (5).numerator
    5
    >>> (5).real
    5
    

    They're not some kind of universal attribute or anything. The int type just implements descriptors for those attributes, for interoperability with other numeric types. Specifically, they were implemented to conform to PEP 3141.