I have not seen a way to do this. I am in Python 3.6.1 (v3.6.1: 69c0db5050, March 21, 2017 01:21:04). MacOS under Sierra, although we will need this to work in Python 2.
I have a custom class that does something that looks like an int with a subfield extension. For my own reasons, I want to be able to do something like
inst * 4
and
inst.subfield << 1
(where the subfield is an inst attribute). These objects are heavily overloaded and, for example, inst print will unload subfields for viewing.
All this is done by overloading all user functions to process math and interacting with other objects. In general, it works very well, with one glaring exception: print. For the most part, the user can forget that this is not an integer and use it as one, but using integer print commands will not work:
print("%#x"%inst) TypeError: %x format: an integer is required, not CustomType
I have __int__ overloaded, and int(inst) returns an integer as expected.
Is there any way to make this work? This is a slight annoyance, but I would like to fix it.
In addition, I have __format__ . So, '{0:x}'.format(inst) works, but the text above does not work.
Thanks!
source share