I’m trying to parse a tuple of the form:
a=(1,2)
or
b=((1,2), (3,4)...)
where for a’s case the code would be:
x, y = a
and b would be:
for element in b:
x, y = element
is there an fast and clean way to accept both forms? This is in a MIDI receive callback
(x is a pointer to a function to run, and y is intensity data to be passed to a light).
This basically checks to see if the first element of the input sequence is iterable. If it is, then it’s your second case (since a tuple is iterable), if it’s not, then it’s your first case.
If you know for sure that the inputs will be tuples, then you could use this instead:
Depending on what you want to do, your handling for the ‘a’ case could be as simple as bundling the single tuple inside a larger tuple and then calling the same code on it as the ‘b’ case, e.g…
Edit: as pointed out in the comments, a better version might be…