The Voidspace Techie Blog

Gravatar The "string" vs. "sequence" problem in Python is particularly vexing. While it's very elegant and clean to think of strings as a sequence of substrings, it presents all kinds of problems in the all-to-frequent situations where you want to allow passing a single string-like-object or a sequence of string-like-objects. Differentiating between these two cases without resorting to isinstance() is unpleasant because a string "is-a" sequence. There are some tricks that can be used (obj[0] is obj[0][0] is obj[0][0][0]), but usually just doing an isinstance(obj, basestring) catches the common cases with minimal thought. Trickier, of course, is the single-or-list problem for arbitrary objects.


Gravatar Well this is exactly what interfaces are hopefully trying to achieve.

If I had to do it in my project I would suggest 'defineing' your accepted sequence type as either an isinstance of dict or of implementing an interface (using one of the popular interfaces library)


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan