summary: (was: callback + __call__ + TypeError: unbound method; on non method)

Volker Apelt gq437x at
Wed Sep 25 10:05:42 CEST 2002

## Thank you, Alex Martelli for your help on this problem. 

  Python 2.1.1. took a reference to a function
  stored in a class attribute as a class method.
  Calling that function through an object instance 
  was not possible. 

Solution for 2.2 + Workaround for python < 2.2:
  In Python 2.2 use staticmethod(). 
  For Python < 2.2 (eg: tested with 2.1.1) use a 
  class replacement for staticmethod and add a second
  group of () to the call. 


import time

def call_me():
    return time.time

## workaround for python < 2.2   
class staticmethod:
    def __init__(self, func): self.func = func
    def __call__(self, *args, **kwds): return self.func(*args, **kwds)
class Callback_good:
    _default_Func = staticmethod(call_me) #class wide default callback function
    def __init__(self): 
        """ """
        self._m_func = Callback_good._default_Func
    def __call__(self): 
        """ """
        # uncomment one of these
        #return self._m_func()() # for python < 2.2
        #return self._m_func()   # for ptyton >= 2.2

x = Callback_good()

## both prints are equal
print time.time 
print x()

Volker Apelt                   

More information about the Python-list mailing list