def safe_float(obj): try: retval = float(obj) except (ValueError, TypeError): retval = 'argument must be a number or numeric string' return retval safe_float('Spanish Inquisition') safe_float([]) print safe_float('1.6') print safe_float(1.6) print safe_float(932)