passing no arguments to a callback function question

matthew matthew at newsgroups.com
Tue Apr 8 05:53:37 EDT 2003


Peter Hansen wrote:
> matthew wrote:
> 
>>I'm pretty new to python and most of my stumblings get me somewhere but
>>I'm stumped on this. Given the following code/exception how do I avoid
>>the exception generated when notify_listeners is called?
>>
>>     def print_params():
>>        print 'PARAMS: ', params
>>
>>gives:-
>>
>>   File "D:\Python22\Lib\site-packages\TeePee\notifier.py", line 39, in
>>__call__
>>     return self.fn(*(self.args + args), **d)
>>TypeError: print_params() takes no arguments (1 given)
<snip>
Peter
Thanks for the hint. However, functions like print_params are outside of 
the class scope sitting in another module. The idea is that users can 
write their own functions which they can add as listeners for certain 
event types being scanned from a file. I 'pinched' the curry class from 
the py.cookbook and I think I'm probably being tripped up by my only 
vague understanding of argument passing. Here is the listener code 
(which I'm sure must look pretty ugly to you guys):-. Thanks for further 
assistance. matthew.

     class curry(object): <<< copied from cookbook - have no idea how it 
works!

         def __init__(self, fn, *args, **kw):
             self.fn, self.args, self.kw = (fn, args, kw)

         def __call__(self, *args, **kw):
             if self.kw:
                 d = self.kw.copy()
                 d.update(kw)
             else:
                 d = kw
             return self.fn(*(self.args + args), **d)

     def add_listener(self, event, function, args=None):
         if debug=='on': print 'Event & Func: ', event,function,args
         try:
             entry = (function, self.curry(function,args))
             self.event_list[event].append(entry)
         except KeyError:
             if debug=='on': print 'Notifier.add_listener: \'', event, 
'\' not in event_list'
             else: raise # to caller

     def call_listeners(self, event):
         try:
             for listener in self.event_list[event]:
                 listener[1]() #called
         except KeyError:
             if debug=='on': print 'Notifier.call_listeners: \'', event, 
'\' not in event_list'
             else: raise # to caller





More information about the Python-list mailing list