[Python-Dev] concerns regarding callable() method

skip at pobox.com skip at pobox.com
Mon Apr 9 04:53:09 CEST 2007


    Guido> My point is that it's futile to use callable() -- even if it
    Guido> passes, you have no assurance that you actually have a valid
    Guido> callback. So why bother with it at all? It's counter to the
    Guido> spirit of Python. If someone passes you a bad callback, they will
    Guido> see a traceback when you call it. Then they fix their
    Guido> program. That's how it's supposed to work.

There's one place where I find the traceback somewhat unhelpful.  Consider
calling a method of a class with incorrect arguments:

    >>> class C:
    ...   def __init__(self):
    ...     pass
    ... 
    >>> C(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __init__() takes exactly 1 argument (2 given)

While in this example it's clear what method wasn't called correctly, a
callback called with the wrong number of arguments yields a fairly useless
stack trace.  I'm thinking in particular of callbacks called from C code
(e.g. Gtk signal handlers).  I think it would be helpful to check to see if
the function being called had an "im_class" attribute.  If so, then resolve
the class name and include it in the TypeError message:

    TypeError: C.__init__() takes exactly 1 argument (2 given)

Skip


More information about the Python-Dev mailing list