Partial Function Application and implicit self problem

Gabriel Rossetti gabriel.rossetti at mydeskfriend.com
Thu Mar 27 04:40:30 EDT 2008


Diez B. Roggisch wrote:
> Gabriel Rossetti schrieb:
>   
>> 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?
>>     
>
> Use a bound-method instead. That has the self already bound to it. Like 
> this:
>
>
> class Foo:
>      def m(self, arg):
>          print arg
>
> f = Foo()
> partial(f.m, 10)
>
>
> Diez
>   

Ok, thanks, I moved the partial defs to __init__() and now it seams to 
work (I still have a but elsewhere, but this part no longer gives an error :


def __init__():
    self.registerServiceAtomic = partial(self.__registerService, True)
    self.registerServiceNonAtomic = partial(self.__registerService, False)

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)


Thanks,
Gabriel



More information about the Python-list mailing list