Why doesn't __call__ lead to infinite recursion?

Aahz aahz at pythoncraft.com
Sat Aug 16 01:16:26 EDT 2003


In article <2a82921f.0308151158.b8c9154 at posting.google.com>,
Patrick Lioi <patrick at novaroot.com> wrote:
>
>def foo(): pass
>
>foo is a function
>foo is a callable object
>foo has method __call__ defined
>
>foo.__call__ is a function
>foo.__call__ is a callable object
>foo.__call__ has method __call__ defined

You're mixing up the distinction between objects and types.  If you do 

    print foo.__dict__

you'll see that __call__ isn't there.  However, if you try

    print type(foo).__dict__

you'll see __call__ there.  When you do foo(), Python actually does
type(foo).__call__(foo).  Because type(foo).__call__ is manipulating
foo, you don't get the circular reference.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

This is Python.  We don't care much about theory, except where it intersects 
with useful practice.  --Aahz




More information about the Python-list mailing list