Why aren't colons optional?

Roy Smith roy at panix.com
Sun Jan 20 14:43:24 EST 2002


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.

I do find the inconsistancy between writing dictionaries with {} and 
accessing them with [] to be a little odd.  Or, maybe it's consistant that 
[] means "access an item of a composite data structure", but then the 
inconsistancy becomes why is [] also used to *define* a list?

Or, maybe the inconsistancy is in that () is used for function call instead 
of []?  Isn't "foo(bar)" really just saying "give me back what you get when 
you apply bar to foo"?  How is that any different from foo[bar] meaning 
"give me back what you get when you apply bar to foo"?  In the latter case, 
how to "apply" bar is determined by the type of foo (list, tuple, or 
dictionary).  Wouldn't it have made things more regular to have generalized 
that to include functions?

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.

On the other hand, it says something for the basic design of the language 
that we need to dig this deep to find warts to complain about :-)



More information about the Python-list mailing list