summary: (was: callback + __call__ + TypeError: unbound method; on non method)
Alex Martelli
aleax at aleax.it
Wed Sep 25 05:04:08 EDT 2002
<posted & mailed>
Volker Apelt wrote:
> ## Thank you, Alex Martelli for your help on this problem.
You're welcome, but:
...
> class replacement for staticmethod and add a second
> group of () to the call.
The "second group of ()" is needed only because:
> def call_me():
> return time.time
CALLING call_me returns (doesn't CALL) the function object
time.time. Therefore:
> class Callback_good:
> _default_Func = staticmethod(call_me) #class wide default callback
now Callback_good._default_Func is a callable returning a
callable when called, therefore:
> 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
Nope -- with your current definitions you need ()() in any
version of Python.
I have another preference, anyway:
class Callback_better:
_m_func = staticmethod(call_me()) # record the final-callable
def __call__(self): return self._m_func()
No need for __init__: self._m_func defaults to Callback_better._m_func
unless specifically set as an instance attribute of self.
This works either in 2.2, or 2.1 with the staticmethod class I
proposed (comes from the Python Cookbook, by the way -- both the
online version, and the enhanced one we edited into an O'Reilly
printed book:-0).
Alex
More information about the Python-list
mailing list