[Python-Dev] Missing operator.call
Guido van Rossum
guido at python.org
Wed Feb 4 20:04:48 CET 2009
I believe the omission of call from the operator module is an
oversight, perhaps caused by the existence (when the operator module
was created) of apply. Since apply has been removed from 3.0, we
should add operator.call (with the same signature) back. It should be
a straightforward wrapper around one of the PyObject_Call* functions.
Also note that using __call__ can lead to all sorts of problems when
looking at classes -- all class objects have a __call__ attribute,
because their metaclass (type) has one, but should not be confused
with a __call__ method defined in the class. A quick illustration:
>>> class C(object):
... def __call__(self): print "instance call"
...
>>> C()
<__main__.C object at 0xf7f9052c>
>>> C()()
instance call
>>> C.__call__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method __call__() must be called with C instance as
first argument (got nothing instead)
>>> C.__call__(C())
instance call
>>> C.__class__.__call__(C)
<__main__.C object at 0xf7f9048c>
>>>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list