[翻译]  Check if input is a list/tuple of strings or a single string

[CHINESE]  检查输入是字符串的列表/元组还是单个字符串


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?).

我希望能够接受单个条目的标准字符串或unicode字符串,以及多个列表或元组,所以isinstance似乎不是答案,除非我错过了一个巧妙的技巧(比如服用)共同祖先类的优势?)。

Python version is 2.5

Python版本是2.5

8 个解决方案

#1


52  

You can check if a variable is a string or unicode string with

您可以检查变量是否为字符串或unicode字符串

isinstance(some_object, basestring)

This will return True for both strings and unicode strings

这将为字符串和unicode字符串返回True

Edit:

编辑:

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

字符串可能不是一个字,但我希望你明白这个想法

#2


12  

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'])

#3


5  

As I like to keep things simple, here is the shortest form that is compatible with both 2.x and 3.x:

因为我喜欢简单易懂,所以这里是与2.x和3.x兼容的最短形式:

# trick for py2/3 compatibility
if 'basestring' not in globals():
   basestring = str

v = "xx"

if isinstance(v, basestring):
   print("is string")

#4


5  

isinstance is an option:

isinstance是一个选项:

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

#5


4  

>>> type('abc') is str
True
>>> type(['abc']) is str
False

This code is compatible with Python 2 and 3

此代码与Python 2和3兼容

#6


3  

Check the type with isinstance(arg, basestring)

使用isinstance检查类型(arg,basestring)

#7


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?

你考虑过varargs语法吗?我不确定这是不是你要问的问题,但是这个问题是否符合你的要求?

#8


-1  

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.

如果输入是元组或列表,它将回复。唯一的问题是,它只能与仅包含一个变量的元组一起正常工作。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号