python - Checking the type of parameter entered into a function -
so want create function , have perform differently based on type of parameter passed in. want 1 thing if string entered, , else if list entered. possible in python? , if how go doing it?
at moment i've tried using isinstance() doesn't seem hitting of if statements:
def tester(*args): if (isinstance(args, str)): return "string" elif (isinstance(args, list)): return"list" else: return "you dun goofed" edit: user ever passing in 1 argument @ time, either list or string.
looks passing single argument function. in case remove '*' since considered tuple of arguments. variable-length arguments , used when aren't sure of number of arguments function need.
def tester(args): if (isinstance(args, str)): return "string" elif (isinstance(args, list)): return"list" else: return "you dun goofed" if function requires variable length parameters , want check whether first parameter being passed string or list, can do:
def tester(*args): if (isinstance(args[0], str)): return "string" elif (isinstance(args[0], list)): return"list" else: return "you dun goofed"
Comments
Post a Comment