Partial Function Application and implicit self problem

Diez B. Roggisch deets at nospam.web.de
Thu Mar 27 05:32:07 EDT 2008


Gabriel Rossetti wrote:

> 
> Gabriel Rossetti wrote:
>> 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?
>>
>> Thanks,
>> Gabriel
>>   
> I also tried this :
> 
> def __registerService(atomic, self, 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)
> 
> thinking that the self will be passed as the first argument of the
> registerService* calls and thus the second argument to the
> curried/partial method, but it doesn't work either, I get :
> 
> Traceback (most recent call last):
>   File "/home/xxx/Documents/Code/Python/test/test_serv.py", line 245, in
> test_registeration
>     self.f.registerServiceAtomic(self.serv)
> exceptions.TypeError: __registerService() takes exactly 3 arguments (2
> given)

First, passing self not as first argument is not going to work, and a HUGE
design flaw anyway: even IF it would work, it would mean that you *must*
use partial to bind default-arguments before invoking a method.

And it goes to much against the design of python.

Apart from that, please post self-contained examples that produce the error.
the above code is hard to grasp because it's unclear which is called from
where with what.

Diez



More information about the Python-list mailing list