0

This might be easy but I'm not sure how to do that. I have a method looking like this:

def my_method(
    self,
    coords: Union[Tuple[int, int], Iterable[Tuple[int, int]]
):
    ...

that is, my_method needs a tuple of integers or something like a list of tuples of integers (those are some sort of coordinates). Is my typing correct for this? But my main question, how do I check whether the input is only one tuple like (1,1) or something like [(1,1),(1,2),..] or ((1,1),(1,2),..) or something similar? I heard about isinstance, but I couldn't figure out how to use this here. Any help is greatly appreciated.

schoeni
  • 65
  • 9
  • `[1, 1]` isn't a tuple. – user2357112 Jun 24 '21 at 07:11
  • Oh yeah. my mistake. I will edit that. – schoeni Jun 24 '21 at 07:15
  • 1
    I think it would be cleaner to make the method only accept iterables of iterables. For example when you want to pass one set of coordinates you pass `[(1,1)]`. You then unpack it within the method body. This way you only have to consider one case in the method. – thisisalsomypassword Jun 24 '21 at 07:21
  • Thanks for the suggestion! I thought about that but I wasn't sure if that would be best practice in this case. Doing it like this would be easier on my side for sure though. – schoeni Jun 24 '21 at 07:24

0 Answers0