I am writing a custom encoding function for use with msgpack-python. I wish to convert any numpy.float object to float before letting msgpack-python serialise it. My encoding function looks like this:
def encode_custom(obj):
if issubclass(obj.__class__,np.float):
obj = float(obj)
return obj
which works just fine. However, the top voted answer at How do I check (at runtime) if one class is a subclass of another? suggests that this is a bad idea. I assume this is because this method doesn't use duck-typing.
Is there a way to duck-type the encoding function?
EDIT: Note that I only want float-like objects to convert to float. Objects that are better represented as another type (e.g. ints) should use that other type, even if they can be float()
'd into a float object.