Steven D'Aprano <steve@pearwood.info> wrote:
To properly handle int and float subclasses, int(), index(), and float() will be called on the objects intended for (d, i, u), (b, o, x, X), and (e, E, f, F, g, G).
-1 on this idea.
This is a rather large violation of the principle of least surprise, and radically different from the behaviour of Python 3 str. In Python 3, '%d' interpolation calls the __str__ method, so if you subclass, you can get the behaviour you want:
py> class HexInt(int): ... def __str__(self): ... return hex(self) ... py> "%d" % HexInt(23) '0x17'
which is exactly what we should expect from a subclass.
You're suggesting that bytes should ignore any custom display implemented by subclasses, and implicitly coerce them to the superclass int. What is the justification for this? You don't define or even describe what you consider "properly handle".
The proposed behavior (at least as I understand it and as I've implemented in my proposed patch) matches Python 2 str/unicode and Python 3 str behavior for these codes. If you want to allow subclasses to have control or to use duck-typing, you have to use str and __format__. I'm okay with the limitation, bytes formatting can be simple, limited and fast. Neil