manipulating files within 'for'

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Sep 12 14:50:20 EDT 2008


Ben Keshet:
> ...wrong.  I thought I should omit the comma and didn't put it.  I guess
> that stating the obvious should be the first attempt with beginners like
> me.  Thanks for thinking about it (it's running perfect now).

In CLisp, Scheme etc, lists such commas aren't necessary, but in
Python if you don't separate strings with a comma they become merged
into a single string:

>>> 'foo', 'bar'
('foo', 'bar')
>>> 'foo' 'bar'
'foobar'

Your mistake is caused by Python not following one of its general
rules:

Explicit is better than implicit.

In such case the string concatenation (+) is done implicitly. It's a
little handy feature once in a while (but not for me so far), while it
may cause bugs, so I don't like this little feature of Python and I
may like to see it removed, because it may bite you in similar
situations, where you forgot a comma for mistake:

parts = ["foo", "bar" "baz"]

Bye,
bearophile



More information about the Python-list mailing list