You can use exception handling; try calling iter()
on the object, for example
try:
iter(a)
except TypeError:
# not an iterable, assume a single value
a = (a,)
If you were planning to iterate over the tuple to handle values anyway, you just store the output of the iter()
call and use it directly:
try:
a = iter(a)
except TypeError:
a = (a,)
for elem in a:
You can also make your function signature accept multiple parameters:
def foobar(*a):
for elem in a:
Now a
is always a tuple, but to pass in a tuple you'd have to call it with:
sometuple = (1, 2, 3)
foobar(*sometuple)
while a single value is passed in as:
foobar(singlevalue)