List/Tuple bug or feature?

Steve Holden sholden at holdenweb.com
Sun Sep 29 06:29:02 EDT 2002


"Mahesh Padmanabhan" <news at nospam.eml.cc> wrote ...
>
> Hi,
>
> I spent a lot of frustrating hours tracking this down and I am
> confused as to why I see this behavior.
>
> >>> x = ('a' 'b')
> >>> x
> 'ab'
>
The two adjacent string constants are concatenated. You don't get a tuple
because there is no comma folowing the singleton element, so technically the
RHS in the assignment is simply a parenthesised expression.

> >>> x = ['a' 'b']
> >>> x
> ['ab']
>
Here no commas are needed because there is no ambiguity. Again, the strings
are concatenated by Python. This allows you to more easily specify strings
in chunks.

> As you might have guessed, I missed a comma between two elements in
> a fairly large list.
>
> If this is a concatenation feature, how can I prevent something like
> this happening in the future ?
>
Don't miss out the commas! It *is* a feature, and it *will* bite you if you
use incorrect syntax.

> This is with Python 2.1.3 on Debian Linux.
>
But is valid for everything since 1.5.2 in all known implementations, I
would guess.

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list