I have the following problem:
There is a function to which I have to pass some arguments.
These arguments are typically stored in tuples and I'm using *args
to receive the arguments.
What is the most elegant way to check if the original argument was just a string?
The problem with the code below is that every character is individually captured by *args
because the ()
do not create a tuple for the 2nd entry.
def fun(*args):
for arg in args:
print(arg)
if __name__ == "__main__":
l = [(0, 1),
("some string"), ]
for i in l:
fun(*i)
print('---')
# should return:
# 0
# 1
# ---
# "some string
# ---
one solution would be to simply add a ,
after ("some string",)
but that is easy to forget if done manually and the argument list relies on external tools and software beyond my control.
Is there a better way to do this?