Callable or not callable, that is the question!

Jason Swails jason.swails at gmail.com
Thu Jul 11 16:28:34 CEST 2013


On Thu, Jul 11, 2013 at 9:05 AM, Ulrich Eckhardt <
ulrich.eckhardt at dominolaser.com> wrote:

> Hello!
>
> I just stumbled over a case where Python (2.7 and 3.3 on MS Windows) fail
> to detect that an object is a function, using the callable() builtin
> function. Investigating, I found out that the object was indeed not
> callable, but in a way that was very unexpected to me:
>
>     class X:
>         @staticmethod
>         def example():
>             pass
>         test1 = example
>         test2 = [example,]
>
>     X.example() # OK
>     X.test1() # OK
>     X.test2[0]() # TypeError: 'staticmethod' object is not callable
>

Interestingly, you can actually use this approach to 'fake' staticmethod
before staticmethod was even introduced.  By accessing example from inside
the test2 class attribute list, there is no instance bound to that method
(even if an instance was used to access it).

Using Python 3.3:

Python 3.3.2 (default, Jun  3 2013, 08:29:09)
[GCC 4.5.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...     def example(): pass
...     test = example,
...
>>> X.test[0]()
>>>

Using Python 2.0 (pre-staticmethod):

Python 2.0.1 (#1, Aug 28 2012, 20:25:41)
[GCC 4.5.3] on linux3
Type "copyright", "credits" or "license" for more information.
>>> class X:
... def example(): pass
... test = example,
...
>>> X.test[0]()
>>> staticmethod
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: There is no variable named 'staticmethod'

Once you change test into an instance attribute, you get back to the
expected behavior

Python 3.3.2 (default, Jun  3 2013, 08:29:09)
[GCC 4.5.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...     def example(self): pass
...     test = example,
...
>>> inst = X()
>>> inst.example()
>>> inst.test[0]()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: example() missing 1 required positional argument: 'self'
>>> inst.test = inst.example,
>>> inst.test[0]()
>>>

All the best,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130711/c08dc66d/attachment.html>


More information about the Python-list mailing list