binding more than one attribute in a facntion

L

linuxnow

I want to have a bound method that "fixes" more than one parmeter of a
funtion. LEt me post an example.

def f(a, b, c):
return a + b + c

I can do:
fplus10 = f(10)
and then call f with 2 params and it works.

But, how can I fix 2 params:
fplus10plus20 = f(10,20)
ignores the second param.

fplus10plus20= fplus10(20)
does not work either.

Can anybody show what I'm doing wrong?
 
B

bearophileHUGS

(e-mail address removed):
def f(a, b, c): return a + b + c
I can do:
fplus10 = f(10)
and then call f with 2 params and it works.

If you call that f or that fplus10 with two parameters you obtain an
error in both cases. You have an error with the f(10) line too.

With Python 2.5 you can probably use the partial(), but for now you can
simply do:

def f(a, b, c):
return a + b + c
def fplus10(b, c):
return f(10, b, c)
def fplus10plus20(c):
return fplus10(20, c)

print f(10, 20, 30)
print fplus10(20, 30)
print fplus10plus20(30)

Bye,
bearophile
 
P

Peter Otten

I want to have a bound method that "fixes" more than one parmeter of a
funtion. LEt me post an example.

def f(a, b, c):
return a + b + c

I can do:
fplus10 = f(10)
and then call f with 2 params and it works.

But, how can I fix 2 params:
fplus10plus20 = f(10,20)
ignores the second param.

fplus10plus20= fplus10(20)
does not work either.

Can anybody show what I'm doing wrong?

Bound methods are limited to one implicit parameter. What you need is
partial function application:
.... return a + b + c
........ def g(*more):
.... return f(*args+more)
.... return g
....6

See http://www.python.org/dev/peps/pep-0309/ for more.

Peter
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top