I would like to know what would be the most suited method to dump an object content?
So I can have any kind of objects. The example below is just an example and it is derived from io.Bytes just to have something to dump that are bytes.
I am clearly wondering whether repurposing the __str__ method is a good idea or not. Choosing between to_bytes or dump is more a consensual question and I think to_bytes would be more understandable.
Use __str__:
def Foo(io.Bytes):
def __str__(self):
self.seek(0)
return self.read()
foo = Foo()
content = bytes(foo)
Use method to_bytes
def Foo(io.Bytes):
def to_bytes(self):
self.seek(0)
return self.read()
foo = Foo()
content = foo.to_bytes()
Same as above, but with method dump
foo = Foo()
content = foo.dump()