Correct way to detect sequence parameter?

后端 未结 12 1553
情书的邮戳
情书的邮戳 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 13:55

    I'm new here so I don't know what's the correct way to do it. I want to answer my answers:

    The problem with all of the above mentioned ways is that str is considered a sequence (it's iterable, has __getitem__, etc.) yet it's usually treated as a single item.

    For example, a function may accept an argument that can either be a filename or a list of filenames. What's the most Pythonic way for the function to detect the first from the latter?

    Should I post this as a new question? Edit the original one?

提交回复
热议问题