How to write a function on python that accepts two objects in input and gives on output minimal type which both of objects can be presented?
Example: we have two objects: 1 and 5.25.We can't convert them both to int because then we will miss information about 5.25 that will be converted to 5. We can convert them both to float: 1.0 and 5.25 and that is correct answer. Sure we can say that we can convert them both to str: "1", "5.25", but in our interpretation we suppose that int < float < tuple < list < str (of course we can't compare types of objects, but we suppose that to get an answer) and then float is the minimum available type that both objects can be converted to.
I tried something like:
def type_convertion(first, second): data_types = [int, float, tuple, list, str] times = {} for _type in data_types: times[_type] = 0 try: if isinstance(_type(first), _type): times[_type] += 1 except TypeError: del times[_type] continue try: if isinstance(_type(second), _type): times[_type] += 1 except TypeError: del times[_type] continue return times.keys() But when I compare int and float the answer is int but should be float and I don't know how to fix it.
isinstance(_type(x), _type)is pretty pointless, either it raises an exception or it isTrue.isinstance(x, _type)instead, if you want to test ifxis already of the given type, not if it can be converted to that type.