Phillip J. Eby
pje at telecommunity.com
Mon Feb 21 04:44:44 CET 2005
At 04:32 PM 2/21/05 +1300, Greg Ewing wrote:
>Alex Martelli wrote:
>>The need to check "is this thingy here string-like" is sort of frequent,
>>because strings are sequences which, when iterated on, yield sequences
>>(strings of length 1) which, when iterated on, yield sequences ad infinitum.
>Yes, this characteristic of strings is unfortunate because it
>tends to make some degree of LBYLing unavoidable.
FWIW, the trick I usually use to deal with this aspect of strings in
recursive algorithms is to check whether the current item of an iteration
is the same object I'm iterating over; if so, I know I've descended into a
string. It doesn't catch it on the first recursion level of course (unless
it was a 1-character string to start with), but it's a quick-and-dirty way
to EAFP such algorithms.
More information about the Python-Dev