I'm trying to force Python 2.7 to print a formatted string for a ctypes POINTER(<type>). I figured I'll write a class that inherits from POINTER and overload __str__. Unfortunately running this piece of code:
from ctypes import *
pc_int = POINTER(c_int)
class PInt(pc_int):
def __str__(self):
return "Very nice formatting ", self.contents
print pc_int(c_int(5))
print PInt(c_int(5))
fails with such exception
$ python playground.py
<__main__.LP_c_int object at 0x7f67fbedfb00>
Traceback (most recent call last):
File "playground.py", line 9, in <module>
print PInt(c_int(5))
TypeError: Cannot create instance: has no _type_
Does anyone know how to cleanly achieve the anticipated effect or what this exception means?
There's only 1 google search result for "TypeError: Cannot create instance: has no type" and it isn't that helpful.
Thanks!