Partial Function Application and implicit self problem

G

Gabriel Rossetti

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
 
D

Diez B. Roggisch

Gabriel said:
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
 
G

Gabriel Rossetti

Diez said:
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
 
G

Gabriel Genellina

En Thu, 27 Mar 2008 05:40:30 -0300, Gabriel Rossetti

I see that you've already solved your original problem. But looking at
those names, I wonder if you're using the mutex module; note that such
"mutex" is absolutely useless in a multithreaded program, it doesn't
guarantee mutual exclusion, the "atomic" testandset operation isn't atomic
at all, by example.
For a "real" mutex, use a threading.Lock object.
 
G

Gabriel Rossetti

Bruno said:
Gabriel Rossetti a écrit :

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
 
B

Bruno Desthuilliers

Gabriel Rossetti a écrit :
Ok, thanks, I didn't know you could do that, I though partial was always
left to right

It's left to right for positional arguments. Using named arguments, you
can pass them in whatever order.
(I had read that about curry)

Which 'curry' ?-)
 
C

castironpi

Gabriel Rossetti a écrit :







It's left to right for positional arguments. Using named arguments, you
can pass them in whatever order.


Which 'curry' ?-)

I think 'prefix' should go in there. Can you try: partial( G.f, a, b )
( self= obj )? (If that's too brief let me know.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top