callbacks in python

Larry Bates larry.bates at websafe.com`
Thu Aug 14 00:45:47 CEST 2008


Alexandru Mosoi wrote:
> On Aug 14, 12:02 am, Fredrik Lundh <fred... at pythonware.com> wrote:
>> your use of the word "callback" is a bit unusual, and your example isn't
>> valid Python code, but it looks as if functools.partial might be what
>> you need:
>>
>>      http://docs.python.org/lib/module-functools.html
> 
> my current implementation is very similar to partial() :) (10x, i'll
> use partial from now on). however it seems that I don't understand
> very well positional and keyword arguments in python because I got the
> error described here: http://docs.python.org/ref/calls.html#calls
> (TypeError: f() got multiple values for keyword argument 'a') which
> confused me even more. so if you pass positional and keyword arguments
> to both partial() and function returned the order of passed arguments
> might be different than expected. i was looking for an implementation
> that somehow avoids this confusion.

Positional arguments come first, keyword arguments come second.  You can pick up 
positional keywords as a list and keyword arguments as a dictionary if you want:

def foo(*args, **kwargs):
     for a in args:
        #
        # process positional arguments as a list
        #

     for k,v in kwargs.iteritems():
        #
        # Process keyword arguments by iterating over the dictionary
        #


It is a little hard to understand exactly what you are wanting to do, but it 
sounds like you should probably ONLY use keyword arguments and then there isn't 
any problem with order or missing arguments (if you set appropriate defaults).

-Larry



More information about the Python-list mailing list