Why doesn't __call__ lead to infinite recursion?

Andrew Dalke adalke at mindspring.com
Sat Aug 16 15:38:40 EDT 2003


Aahz:
> No time to investigate further, but all your examples used classic
> classes instead of new-style classes; I'm pretty sure that new-style
> classes will more closely emulate the way functions work.  There's also
> the wrinkle I didn't mention that functions use a dict proxy IIRC.

Interesting.  Very interesting.

>>> class XYZ(object):
...  def __init__(self):
...   def abc(x):
...    print "Hello", x
...   self.__call__ = abc
...  def __call__(self, x):
...   print "Yo", x
...
>>> xyz = XYZ()
>>> xyz("fred")
Yo fred
>>>
>>> getattr(xyz, "__call__")
<function abc at 0x0168CB70>
>>>
I wonder if this will affect any of my code.

It does explain the observed differences better, since FunctionType
in 2.3 is derived from object while my class was not.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list