Check if input is a list/tuple of strings or a single string
Question:
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 strings.
Given that strings act as lists of characters, how can I tell which kind the method has received?
I’d like to be able to accept either standard or unicode strings for a single entry, and either lists or tuples for multiple, so isinstance doesn’t seem to be the answer unless I’m missing a clever trick with it (like taking advantage of common ancestor classes?).
Python version is 2.5
Answers:
Have you considered varargs
syntax? I’m not really sure if this is what you’re asking, but would something like this question be along your lines?
You can check if a variable is a string or unicode string with
- Python 3:
isinstance(some_object, str)
- Python 2:
isinstance(some_object, basestring)
This will return True
for both strings and unicode strings
As you are using python 2.5, you could do something like this:
if isinstance(some_object, basestring):
...
elif all(isinstance(item, basestring) for item in some_object): # check iterable for stringness of all items. Will raise TypeError if some_object is not iterable
...
else:
raise TypeError # or something along that line
Stringness is probably not a word, but I hope you get the idea
Check the type with isinstance(arg, basestring)
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'])
isinstance
is an option:
In [2]: isinstance("a", str)
Out[2]: True
In [3]: isinstance([], str)
Out[3]: False
In [4]: isinstance([], list)
Out[4]: True
In [5]: isinstance("", list)
Out[5]: False
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")
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.
>>> type('abc') is str
True
>>> type(['abc']) is str
False
This code is compatible with Python 2 and 3
I’m surprised no one gave an answer with duck typing, but gave unclear or highly type-dependent or version-dependent answers. Also, the accepted answer unfortunately has separate code for Python 2 and 3. Python uses and encourages duck typing, so (one line more than sorin’s "shortest form" which is not duck typing) I instead recommend:
def is_str(v):
return hasattr(v, 'lower')
…and whatever other attributes you want to use (remember the quotes). That way, client code using your software can send whatever kind of string they want as long as it has the interface your software requires. Duck typing is more useful in this way for other types, but it is generally the best way.
Or you could also do this (or generally check for AttributeError and take some other action):
def is_str(v):
try:
vL = v.lower()
except AttributeError:
return False
return True
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 strings.
Given that strings act as lists of characters, how can I tell which kind the method has received?
I’d like to be able to accept either standard or unicode strings for a single entry, and either lists or tuples for multiple, so isinstance doesn’t seem to be the answer unless I’m missing a clever trick with it (like taking advantage of common ancestor classes?).
Python version is 2.5
Have you considered varargs
syntax? I’m not really sure if this is what you’re asking, but would something like this question be along your lines?
You can check if a variable is a string or unicode string with
- Python 3:
isinstance(some_object, str)
- Python 2:
isinstance(some_object, basestring)
This will return True
for both strings and unicode strings
As you are using python 2.5, you could do something like this:
if isinstance(some_object, basestring):
...
elif all(isinstance(item, basestring) for item in some_object): # check iterable for stringness of all items. Will raise TypeError if some_object is not iterable
...
else:
raise TypeError # or something along that line
Stringness is probably not a word, but I hope you get the idea
Check the type with isinstance(arg, basestring)
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'])
isinstance
is an option:
In [2]: isinstance("a", str)
Out[2]: True
In [3]: isinstance([], str)
Out[3]: False
In [4]: isinstance([], list)
Out[4]: True
In [5]: isinstance("", list)
Out[5]: False
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")
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.
>>> type('abc') is str
True
>>> type(['abc']) is str
False
This code is compatible with Python 2 and 3
I’m surprised no one gave an answer with duck typing, but gave unclear or highly type-dependent or version-dependent answers. Also, the accepted answer unfortunately has separate code for Python 2 and 3. Python uses and encourages duck typing, so (one line more than sorin’s "shortest form" which is not duck typing) I instead recommend:
def is_str(v):
return hasattr(v, 'lower')
…and whatever other attributes you want to use (remember the quotes). That way, client code using your software can send whatever kind of string they want as long as it has the interface your software requires. Duck typing is more useful in this way for other types, but it is generally the best way.
Or you could also do this (or generally check for AttributeError and take some other action):
def is_str(v):
try:
vL = v.lower()
except AttributeError:
return False
return True