() vs.  operator
pavlovevidence at gmail.com
Thu Oct 15 22:19:10 CEST 2009
On Oct 15, 12:14 am, Ole Streicher <ole-usenet-s... at gmx.net> wrote:
> I am curious when one should implement a "__call__()" and when a
> "__getitem__()" method.
> For example, I want to display functions and data in the same plot. For
> a function, the natural interface would to be called as "f(x)", while
> the natural interface for data would be "f[x]". On the other hand,
> whether a certain data object is a function or a data table is just an
> inner detail of the object (imagine f.e. a complex function that
> contains a data table as cache), and there is no reason to distinguish
> them by interface.
> So what is the reason that Python has separate __call__()/() and
> __getitem__()/ interfaces and what is the rule to choose between them?
It's just a language design decision. You can go either way, Python
chose to be like C instead of Fortran or Ada. I've used both kinds of
languages, and I prefer to have an external clue about the nature of
the object I'm dealing with. However, I use many languages that don't
distinguish between the two and it is not that big of a deal, and does
have some small advantages.
More information about the Python-list