I've got a Python function that takes two arguments,
lon. These arguments can be either scalar values (
52.3) or any sort of iterable (e.g. a list or a NumPy array).
At the beginning of my function I need to check what I've been given, and convert both arguments to arrays (if needed) and check that both arguments are the same length.
# Deal with scalar values try: lat_count = len(lat) except: lat = [lat] lat_count = 1 try: lon_count = len(lon) except: lon = [lon] lon_count = 1 if lat_count != lon_count: raise ValueError("lan and lon arrays must be the same length") lat = np.array(lat) lon = np.array(lon)
It feels horribly messy, but I can't work out how to make it better. Any ideas?