-1

Is there something I can return instead of 'None' so that it's still iterable but empty? Sometimes I want to return two values but sometimes I only want to return one.

for distance in range(1, 8):

    temp_cords = [(origin[0] - ((origin[0]-check[0])*distance)), (origin[1] - ((origin[1]-check[1])*distance))]

    if temp_cords in all_locations:
        return False, None #I want to return only 'False' here.

    elif temp_cords in (white_locations + black_locations):

        if white_turn:

            if temp_cords in white_locations:
                return True, (distance - 1) #But here, I want to return two values.
ThePawn08
  • 13
  • 1

1 Answers1

0

It is never a good design to create a function that returns a tuple whose length can vary based on different conditions because the caller would not be able to simply unpack the returning tuple into a fixed number of variables.

In your case, it is better to not return a Boolean but simply return distance - 1 by default, and then either:

  • return None when temp_cords in all_locations is True, so that caller can just check whether if the returning value is None to decide what to do with the returning value
  • or, raise an exception so that the caller can call the function in a try-except block to handle the condition when temp_cords in all_locations is True.
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1
    Even inhomogeneous/optional return type with `None` is a bit sketchy. I'd often prefer to always return an integer, otherwise raise an exception. – wim Mar 07 '23 at 02:08
  • `None` can be useful as a default returning value sometimes when you are sure that the regular returning values of the function can never be `None`, as in the case of the standard `dict.get` method. But I agree that in general an exception handler is the way to go. – blhsing Mar 07 '23 at 02:22