Partial Function Application and implicit self problem

Gabriel Rossetti gabriel.rossetti at mydeskfriend.com
Thu Mar 27 06:15:17 EDT 2008


Bruno Desthuilliers wrote:
> Gabriel Rossetti a écrit :
>> Hello,
>>
>> I am using Partial Function Application in a class and I've come up 
>> with a problem, when the method is called it tries to pass "self" to 
>> the curried/partial function, but this should be the first argument 
>> in reality, but since the function is curried, then the self gets 
>> passed as the second argument. Here is the code :
>>
>> def __registerService(self, atomic, service):
>>    def reg(service):
>>        # registers the service
>>        ...
>>          if(atomic):
>>        # Lock the service dict and register the service, then unlock it
>>        self.__mutex.lock(reg, service)
>>        self.__mutex.unlock()
>>    else:
>>        reg(service)
>>   registerServiceAtomic = partial(__registerService, True)
>> registerServiceNonAtomic = partial(__registerService, False)
>>
>> I should pass self when applying partial, but then I can't do that 
>> since self is not available there. Does anyone have any ideas?
>
> registerServiceAtomic = partial(__registerService, atomic=True)
> registerServiceNonAtomic = partial(__registerService, atomic=False)
>
> Caveat: you'll have to either invert the order of the atomic and 
> service params, or call the partials with named arg for service.
>
> HTH
>
Ok, thanks, I didn't know you could do that, I though partial was always 
left to right (I had read that about curry)

Gabriel



More information about the Python-list mailing list