callback + __call__ + TypeError: unbound method; on non method

Alex Martelli aleax at aleax.it
Tue Sep 24 08:57:13 EDT 2002


Volker Apelt wrote:


> But I can't remove the .__call__ method from my
> Callback class. The method is essential for

The problem is not with method __call__, as it gets called
with an instance.  Rather, it's right here:

> class Callback:
>     """
>     """
>     _default_Func = call_me # class wide default callback function
>     
>     def __init__(self):
>         """ """
>         self._m_func = Callback._default_Func

Here, self._m_func is NOT a function -- it's an unbound method,
thus it's not callable unless the first argument is an instance
of the class (or any subclass thereof).

When you fetch from a class an attribute that is a Python coded
function object, what you get is an unbound method object.  This
behavior is intrinsic to Python's OO mechanisms.

Remedy: have Callback._default_Func be just about ANY callable
except a Python-coded function object:-).  In Python 2.2, the
built-in staticmethod is made just for the purpose:

class Callback:
     """
     """
     _default_Func = staticmethod(call_me) # class wide default callback 

and the rest can stay unchanged (haven't examined it in detail, but
this is the surely-buggy spot in your code and explains the traceback
that you report).


Alex




More information about the Python-list mailing list