Why aren't colons optional?

John Roth johnroth at ameritech.net
Mon Jan 21 10:18:21 EST 2002


"Roy Smith" <roy at panix.com> wrote in message
news:roy-2809B1.14432320012002 at news1.panix.com...
> Robert Amesz <sheershion at mailexpire.com> wrote:
> > So don't complain or question, but *cherish* the consistency of
Python.
> > It is a great bonus.
>
> Even python isn't completely consistant about ":" introducing an
indented
> block.  Consider for example:
>
> x = {'a':
>      1,
>      'b':
>      2
>      }
>
> I have no idea why anybody would want to write it that way, but it's
legal.
> Interestingly enough, emacs's python mode realized that this was a
case
> where ":" didn't start an indented block, and indented the lines as
you see
> above.

This isn't an indented block. It's a continuation. A block consists of
statements, and these are not statements.
>
> I've also always found the interchangability of ' and " to be a bit
> amusing.  Seems like it was just a bone to throw to the "there's more
than
> one way to do it" camp.  At least in the shell, the two types of
quotes
> have slightly different functions, in that one is "stronger" than the
> other.  I suppose it's 20/20 monday night second-guessing, but if ''
had
> been given the function that the ugly r'' construct has, it would have
made
> a bit more sense.  I often joke that the "r" stands for "regex", which
is
> how I most often use it, but I wouldn't be surprised if that's
actually a
> real point of confusion for people learning the language.

There are two points here: one is that having two quote characters
is useful when you have to include text that has embedded quotes.
It eliminates the need for a whole level of backslashes, which in turn
improves program readability. It also avoids subtle errors for
people who are not truely expert at the language, which is true
for many who use it as a scripting language.

The second point is that the 'r"' syntax was added later.

John Roth





More information about the Python-list mailing list