[Python-3000] callable()

Ronald Oussoren ronaldoussoren at mac.com
Thu Jul 20 07:28:31 CEST 2006


On Jul 20, 2006, at 3:26 AM, Greg Ewing wrote:

> Ronald Oussoren wrote:
>
>> Classic classes?
>
> I just checked, and it seems they've been fixed too:
> callable() and hasattr(obj, '__call_') give the same
> result -- true if and only if a __call__ method has
> been defined.

But classic classes theirself are callable yet don't have a __call__  
attribute. New-style classes do have a call method. To expand on my  
previous interpreter session:

Python 2.5b2 (r25b2:50570, Jul 11 2006, 09:46:24)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> class O: pass        # Old-style class
...
 >>> O.__call__             # No __call__
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: class O has no attribute '__call__'
 >>> class N (object): pass      # New-style class
...
 >>> N.__call__             # Has __call__
<method-wrapper '__call__' of type object at 0x60f770>
 >>>
 >>> # Instantition by calling works for both:
 >>> O()
<__main__.O instance at 0x701c0>
 >>> N()
<__main__.N object at 0x6e410>
 >>>


Ronald



More information about the Python-3000 mailing list