The Magick of __call__ (Or, Digging Deeper Than I Ought To)

Chris Angelico rosuav at
Fri Apr 1 22:21:06 EDT 2011

Thanks for those explanations! As Corey's original subject says, this
IS digging pretty deep into implementation details. My geekly side
loves that though!

On Sat, Apr 2, 2011 at 8:35 AM, Terry Reedy <tjreedy at> wrote:
> Every time a method is accessed through an instance, a new wrapper is
> created. Why? 1. If you want to reuse a bound methods, just bind it to a
> name or something and reuse it. 2. To automatically keep it for possible
> reuse, which normally is not too common, it wold have to be kept in some
> hidden dict which would grow indefinitely unless pruned occasionally.

What I was thinking was something along the lines of a loop-back
reference in the wrapper itself. So for instance:

Foo.__call__ = wrapper(Foo)

The wrapper would be created with a __call__ method of itself:
self.__call__ = self

That would not require a dictionary, it's just a special case of
creating the __call__ method. Since, apparently, we can "go infinite"
with the __call__ dereferencing, these objects must be created when

Fascinating stuff!

Chris Angelico

More information about the Python-list mailing list