() vs. [] operator

Ole Streicher ole-usenet-spam at gmx.net
Thu Oct 15 09:14:35 CEST 2009


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?



More information about the Python-list mailing list