callback + __call__ + TypeError: unbound method; on non method
blackhole at notformail.invalid.de
Tue Sep 24 11:46:23 EDT 2002
Alex Martelli <aleax at aleax.it> writes:
> 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).
Let me rephrase what you stated above. Callback._default_Func is
*always* a method. Thats the way python understands this code.
So Python below v2.2 can not store a function reference in a
class static variable (c++ speak) and use it later as for function
So, I have to upgrade to v2.2 and write:
_default_Func = staticmethod(call_me)# class wide default callback function
self._m_func = staticmethod(Callback._default_Func)
self._m_func() # << this will call call_me() ???
Thank you Alex,
More information about the Python-list