aahz at pythoncraft.com
Fri Jun 15 05:48:33 CEST 2007
In article <1180887490.024955.61380 at p47g2000hsd.googlegroups.com>,
George Sakkis <george.sakkis at gmail.com> wrote:
>On Jun 2, 4:58 pm, a... at pythoncraft.com (Aahz) wrote:
>> In article <1180807432.351720.123... at p47g2000hsd.googlegroups.com>,
>> George Sakkis <george.sak... at gmail.com> wrote:
>>>- Strings being iterable; unfortunately this will stay in Py3K.
>> I'll repeat the comment I made on python-3000:
>> "...string iteration isn't about treating strings as sequences of
>> strings, it's about treating strings as sequences of characters. The
>> fact that characters are also strings is the reason we have problems,
>> but characters are strings for other good reasons."
>No, the reason we have problems is that far more often than not
>strings are treated as atomic values, not sequences of smaller strings
>or characters. A classic example is flatten(), where most people are
>surprised if flatten([1, (3.14, 'hello')]) returns [1, 3.14, 'h', 'e',
>'l', 'l', 'o'].
Enh. That's not my experience -- the ability to slice, dice, and
concatenate strings is intrinsic to their usefulness.
>> Thing is, the fact that you can e.g. slice strings just like other
>> sequence types creates the consequence that you can also iterate over
>> strings -- moreover, some of us actually do iterate over strings (though
>> of course we could if necessary create lists/tuples of characters). In
>> the grand scheme of things, I rarely see people running into problems
>> with iterating over strings.
>One class of problems is functions such as flatten() that expect "a
>collection or an atom", with strings being typically considered
>atomic. A second common pitfall are functions that expect file-like
>objects but are given file names instead:
> for line in input_file:
>process_file('/home/george/.bashrc') # oops
That's a problem, yes, and it has oddly enough gotten worse since
iterators were introduced into Python. Nevertheless, I have yet to see
anyone suggest a mechanism for fixing this particular gotcha without
creating more problems. Strings are just too useful as sequences.
Moreover, my experience is that these kinds of problems don't show up
all that frequently in practice.
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"as long as we like the same operating system, things are cool." --piranha
More information about the Python-list