How can I tell if a python variable is a string or a list?

后端 未结 8 1742
孤独总比滥情好
孤独总比滥情好 2020-12-02 19:45

I have a routine that takes a list of strings as a parameter, but I\'d like to support passing in a single string and converting it to a list of one string. For example:

相关标签:
8条回答
  • 2020-12-02 20:31
    isinstance(your_var, basestring)
    
    0 讨论(0)
  • 2020-12-02 20:33
    def func(files):
        for f in files if not isinstance(files, basestring) else [files]:
            doSomethingWithFile(f)
    
    func(['file1', 'file2', 'file3'])
    
    func('file1')
    
    0 讨论(0)
提交回复
热议问题