I have a custom class, which has some string data. I wish to be able to save this string data to a file, using a file handle’s write object. I have implemented __str__(), so i can do str(myobject), what is the equivalent method for making python consider my object to be a character buffer object?
I have a custom class, which has some string data. I wish to be
Share
If you are trying to use your object with library code, that expects to be able to write what you give it to a file, then you may have to resort to implementing a “duck file” class that acts like a
filebut supports your stringable object. Unfortunately,fileis not a type that you can subclass easily, at least as of Python 2.6. You will have to implement enough of the file protocol (write,writelines,tell, etc.) to allow the library code to work as expected.