문제

I'm building an SQL query's Regex, which I'm passing to a Python function. Here's what I'm currently doing (iterable is a variable passed to the function):

if isinstance(iterable, (list, tuple, set)):
    app = '|'.join(iterable)

I'd like to be able to test for an abstraction (see the collection module's ABC's) that means that it's a container, but not a basestring. (which is iterable!)

Is there a way to improve on the above?

도움이 되었습니까?

해결책

I don't think any such thing exists - what you have may be the best.

Occasionally, I have functions that take either strings (which i'll split) or an iterable of strings. For those, I usually go for a negative test instead i.e.

if isinstance(iterable, basestring):
    iterable = iterable.split(',')

etc.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top