On Thu, Oct 15, 2009 at 5:14 PM, Ole Streicher <span dir="ltr"><<a href="mailto:ole-usenet-spam@gmx.net">ole-usenet-spam@gmx.net</a>></span> wrote:<br><snip><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
So what is the reason that Python has separate __call__()/() and<br>
__getitem__()/[] interfaces and what is the rule to choose between them?</blockquote><div><br>Hi,<br><br>This is very interesting, a thought that never occured to me before. Usually, a function is a function; an array (list, iterator, whatever you have) is, well, an array of data. You access them by index.<br>
<br>Mathematically speaking, functions are usually continuous. For any given continuous function, you can give it any value of input (1, 0.1, or even 0.000001) and it'll give you one output. But obviously we don't call an item from index 0.1, but 0, 1, 2, 3, and so on. That's probably the first sign where your function might be best fitted.<br>
<br>Also, one would normally expect a __getitem__() call to be very fast; it would be awkward to return A[10000] a hundred times slower than A[1]. That said, it's a simple constant-time look up. That'll give you another clue when to distinquish the two functions.<br>
<br>The other way also works; if a function takes any floating/decimal number, usually we expect it to be callable, with the () operator.<br><br>I can think of funky things like to memorise every function call that has been done, and look up the memory if this same input has been called before; it might speed up some calculation for certain purposes. All in all, a good idea to separate the two operators.<br>
<br>Cheers,<br>Xav<br><br></div></div>