Why aren't colons optional?

Alex Martelli aleax at aleax.it
Sun Jan 20 16:06:58 EST 2002


Roy Smith wrote:
        ...
> 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?

No, it would have substantially impoverished the language:

class doubleface:
    def __getitem__(self, name):
        return "anitem"
    def __call__(self, *args):
        return "acall"

operator [] always calls __getitem__ with a single argument; operator ()
calls __call__.  It's not true that "how to apply bar is determined by the
type" of the LHS operand: it's always a call to __getitem__; of course,
various types are free to override this differently, and do (and similarly
for __call__).

The use of {} for dictionary-displays is indeed a tiny anomaly (but I
have no better idea for the general case; for the reasonably common case 
where keys are constant identifiers, of course,

def dic(**kw): return kw

and, e.g., dic(a=1, b=2, c=3), may be preferable to {'a':1, 'b':2, 'c':3}, 
but that doesn't generalize).  But the distinction between the [] and ()
operators is anything but an "inconsistency".


Alex




More information about the Python-list mailing list