PEP 3141: __ratio__ instead of numerator/denominator

Let me mention that SageMath is also concerned by real and imag that numbers ABC defines as attributes and that SageMath defines as methods. The four numerator/denominator/real/imag have to be thought together.

As Mark Bell mentionned, it seems technically possible to make a transition in SageMath from methods to properties for Integer.numerator and Integer.denominator. But there are much more objects than rational numbers that have a numerator and denominator methods (e.g. rational fractions that are callable, fractional ideals in a Dirichlet domain). This does make a lot of backward incompatible changes. For example the numerator of a rational function is a polynomial which is callable so that my_fraction.numerator()(x=4) will have to be changed for my_fraction.numerator(x=4).

More importantly, the rationale in SageMath is that there is no attribute in the public API. It simplifies by far the usage: you don’t have to guess whether x.foo is an attribute or a method, it is a method. Furthermore, SageMath basic number types do not have Python attributes. They are extension types built from C or C++ types. I dislike the fact that executing x.numerator (that looks like an attribute lookup from a user perspective) would actually trigger computations.

From a SageMath perspective, chaging the Python numbers ABC from “implement a numerator attribute” to “implement a __numerator__ method or attribute” would be a trivial transition. And as the OP mentioned, special attributes/methods in Python are mostly implemented as dunders.

1 Like