[Python-Dev] Missing operator.call

Steven Bethard steven.bethard at gmail.com
Wed Feb 4 21:07:18 CET 2009


On Wed, Feb 4, 2009 at 10:50 AM, Brett Cannon <brett at python.org> wrote:
> On Wed, Feb 4, 2009 at 10:43, Steven Bethard <steven.bethard at gmail.com> wrote:
>> Not sure I follow you here. It's not the __init__ that allows you to
>> do ``x()``, it's the fact that the class declares a __call__, right?
>>
>>>>> class C(object):
>> ...     pass
>> ...
>>>>> C.__call__()
>> <__main__.C object at 0x01A3C370>
>>>>> C()
>> <__main__.C object at 0x02622EB0>
>>>>> str.__call__()
>> ''
>>>>> str()
>> ''
>>
>
> I don't think so::
>
>>>> Foo.__call__
> <method-wrapper '__call__' of type object at 0x81cee0c>
>>>> Foo.__call__ = lambda: None
>>>> Foo.__call__
> <unbound method Foo.<lambda>>
>>>> Foo()
> <__main__.Foo object at 0xf7f90e8c>

Take a look at PyObject_Call in abstract.c. Basically, __call__ is
always looked up on the type, something like:

>>> class C(object):
...     def __call__(self):
...         return 'instance'
...
>>> def func():
...     return 'func'
...
>>> type(C).__call__(C)
<__main__.C object at 0x0263E250>
>>> type(C()).__call__(C())
'instance'
>>> type(func).__call__(func)
'func'


Steve
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list