Correct way to detect sequence parameter?

后端 未结 12 1547
情书的邮戳
情书的邮戳 2020-11-28 13:48

I want to write a function that accepts a parameter which can be either a sequence or a single value. The type of value is str, int, etc., but I don\'t want

12条回答
  •  鱼传尺愫
    2020-11-28 14:18

    You could pass your parameter in the built-in len() function and check whether this causes an error. As others said, the string type requires special handling.

    According to the documentation the len function can accept a sequence (string, list, tuple) or a dictionary.

    You could check that an object is a string with the following code:

    x.__class__ == "".__class__
    

提交回复
热议问题