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

Volker Apelt blackhole at notformail.invalid.de
Tue Sep 24 14:25:53 CEST 2002


Hi,

I have a problem with a stored function in 
a callback class object.  I try to create a class 
(Callback) which accepts a function (a callable) as argument,
stores it in an object attribute (self._m_func) 
and uses it in Callback's.__call__ function.

The function stored is _not_ a method of 
of the calling class.

I tried several types of calls to self._m_func
but I allways end up in 

TypeError: unbound method call_me() must be called with instance as first argument

I tried some other class (XXX) without a .__call__
method, which calls self._m_func and it succeeds. 

But I can't remove the .__call__ method from my 
Callback class. The method is essential for
Callback, because an instance of it is 
used by some wrapped C-API function (not shown) 
to initiate some action on the python side 
of my code.

Do you know what is wrong in the following 
example?

I'm looking for an acceptable workaround or 
fix.

--------------------------  demo code follows
#!/usr/bin/python 
#  -*- python -*- 

# python -v 2.1.1 on linux (Suse 7.3) 
import time

# the called function
def call_me():
    return time.time

# demo: this class works as expected  
class XXX:
    def __init__(self):
        #self.m_func = time.time
        self.m_func = call_me

    def callback_currTime(self):
        return self.m_func()() # becomes self.m_func() with time.time above

    def direct_currTime(self):
        return time.time()

# defekt: this class doesn't    
class Callback:
    """
    """
    _default_Func = call_me # class wide default callback function
    
    def __init__(self):
        """ """
        self._m_func = Callback._default_Func
        
    def __call__(self, case=None):
        """ """
        ## do something before calling back
        ### -- left out
        ## all attempts end in this type error
        ## TypeError: unbound method call_me() must be called \
        ##   with instance as first argument
        ## find a valid form of calling the function stored 
        ## in _m_func 
        ##   fazit: None was found
        if   case is None or case == 1:
            return self._m_func()
        elif case == 2:
            return self.Func()()
        elif case == 3:
            C = self.Func()()
            return C()
        else:
            return "ignored"
        ##

    def useFunc(self,func=None):
        """ """
        if func is None:
            self._m_func = Callback._default_Func
        else:
            self._m_func = func

    def Func(self):
        """ """
        return self._m_func
#
#####

x = XXX()

print x.direct_currTime()
print x.callback_currTime()

x = Callback()

# try all cases 
for c in xrange(5):
    try:
        print x(c)
    except TypeError, e:
        print "CASE ",c,": ",e

##  OUTPUT  of this script 
#1032868956.78
#1032868956.78
#ignored
#CASE  1 :  unbound method call_me() must be called with instance as first argument
#CASE  2 :  unbound method call_me() must be called with instance as first argument
#CASE  3 :  unbound method call_me() must be called with instance as first argument
#ignored

# Local Variables:
#  (auto-fill-mode nil)
# End:
#end of file
---------------------- end of demo code

run it to see the problems.

thanks 


Volker

-- 
Volker Apelt                   





More information about the Python-list mailing list