75

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

dreftymac
  • 31,404
  • 26
  • 119
  • 182
mavnn
  • 9,101
  • 4
  • 34
  • 52

9 Answers9

84

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

Romain Capron
  • 1,565
  • 1
  • 18
  • 23
Steef
  • 33,059
  • 4
  • 45
  • 36
  • Aha! I thought there must be some common ancestry somewhere, I just couldn't find the reference... – mavnn May 28 '09 at 19:42
  • 12
    python 3 has removed `basestring`. Use `str` (or `bytes`, if you have bytes...) instead – spazm Jan 10 '19 at 01:25
  • 1
    Using the `six` library, you can use `six.string_types` in place of `str` or `basestring` for compatibility. – Dakota Aug 27 '20 at 18:13
  • `isinstance` and `type` are to avoid in production due to their performance. Duck typing should be used instead. – bardosd Jun 04 '21 at 08:57
32

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
besen
  • 32,084
  • 1
  • 21
  • 12
20

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'])
FogleBird
  • 74,300
  • 25
  • 125
  • 131
6

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")
sorin
  • 161,544
  • 178
  • 535
  • 806
5
>>> type('abc') is str
True
>>> type(['abc']) is str
False

This code is compatible with Python 2 and 3

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • Seems to be the most concise and clear way, as of today. –  Jul 24 '18 at 05:12
  • With python2, this will miss unicode strings, which is why the python2 examples use `basestring`. e.g. `type(u'abc') is str` -> `False` – spazm Jan 10 '19 at 01:28
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
Poikilos
  • 1,050
  • 8
  • 11
  • But if you're writing an `is_str` function, is it really duck typing at that point? I suppose, but if you need to use `.lower()`, then just use `.lower()` and catch the potential `AttributeError`. No need to waste cycles checking first. The problem is, sometimes you aren't calling any specific string methods. E.g. maybe you are passing the string directly to another function. In an ideal world, *that* function would raise an exception for non-strings, but you can't always count on it. Sometimes, `isinstance()` really is the ideal solution. – Dominick Pastore May 25 '21 at 02:14
  • @DominickPastore Yes, you wouldn't use my function (but it's an example & directly answers the question). Before it I said "or you could..." not that using the function was duck typing. That would be, as you say, to put the body of my function (or do what you are saying or what I said before the function) inline & form code around it. I disagree about `isinstance`--The point of duck typing & why Python uses it is to not error out when an object does what you want to do, but instead handle individual exceptions--If it "quacks like a duck" it is a duck (or you can count on it for the use case). – Poikilos Jun 11 '21 at 03:50
  • Notice I said "put the body of my function" inline, not the definition. You would just use the code from it not define it, then you'd modify the code to do things specific to your code instead of what the function does. – Poikilos Jun 11 '21 at 04:01
3

Check the type with isinstance(arg, basestring)

Ants Aasma
  • 53,288
  • 15
  • 90
  • 97
1

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?

Community
  • 1
  • 1
David Berger
  • 12,385
  • 6
  • 38
  • 51
  • Wouldn't that just force the caller to specify which was being sent? I'd prefer to avoid that if possible. (And I'm also intrigued as to whether what I want to do is possible in an elegant way...) – mavnn May 28 '09 at 19:39
-2

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.

DragonZeith
  • 71
  • 2
  • 10