I\'ve a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strin
Type checking:
def func(arg):
if not isinstance(arg, (list, tuple)):
arg = [arg]
# process
func('abc')
func(['abc', '123'])
Varargs:
def func(*arg):
# process
func('abc')
func('abc', '123')
func(*['abc', '123'])
Can't you do:
(i == list (i) or i == tuple (i))
It would reply if the input is tuple or list. The only issue is that it doesn't work properly with a tuple holding only one variable.
As I like to keep things simple, here is the shortest form that is compatible with both 2.x and 3.x:
# trick for py2/3 compatibility
if 'basestring' not in globals():
basestring = str
v = "xx"
if isinstance(v, basestring):
print("is string")