[issue33002] Making a class formattable as hex/oct integer with printf-style formatting requires both __int__ and __index__ for no good reason

Josh Rosenberg report at bugs.python.org
Mon Mar 5 16:29:44 EST 2018


Josh Rosenberg <shadowranger+python at gmail.com> added the comment:

To be clear, this is a problem with old-style (printf-style) formatting, and applies to both bytes formatting and str formatting. So a class like:

    class Foo:
        def __index__(self):
            return 1

will fail with a TypeError should you do any of:

    '%o' % Foo()
    '%x' % Foo()
    '%X' % Foo()
    b'%o' % Foo()
    b'%x' % Foo()
    b'%X' % Foo()

even though hex(Foo()) and oct(Foo()) work without issue.

----------
title: Making a class formattable as hex/oct integer requires both __int__ and __index__ for no good reason -> Making a class formattable as hex/oct integer with printf-style formatting requires both __int__ and __index__ for no good reason

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33002>
_______________________________________


More information about the Python-bugs-list mailing list