A lot of languages use square brackets for array indexing, and round brackets (also known as parentheses) for function calls. For example, in C:
int array[10];
void foo(int x);
int x = array[3]; // array indexing
foo(42); // function call
However, there is almost never a case where you could use round brackets after an array or square brackets after a function, for example:
int x = array(3); // compile error
foo[42]; // compile error
Even in languages where you can overload both the array indexing and function call operators, it seems very rare to ever do that. So then it seems like this difference in syntax is unnecessary. And indeed, in some languages parentheses are used for both, like in most dialects of BASIC.
Why have square brackets historically been used for array indexing? What are the pros and cons of using a different notation than the one used for function calls?
__call__
method) and square-brackets postfix (the__getitem__
and__setitem__
methods). It would be very unusual for a Python class to override both without giving them different behaviour. $\endgroup$[]
is normally used for an operation that is in some sense analogous to indexing; overloading()
is normally used for an operation that is in some sense analogous to calling; and a type that provided both would ordinarily definitely not make them the same. $\endgroup$+(x, y)
or(+ x y)
, but a syntax being more uniform does not tend to make it more understandable. $\endgroup$