473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

binding more than one attribute in a facntion

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?

Jul 26 '06 #1
4 1127
li******@gmail.com:
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

Jul 26 '06 #2
li******@gmail.com wrote:
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:
>>def f(a, b, c):
.... return a + b + c
....
>>def partial(f, *args):
.... def g(*more):
.... return f(*args+more)
.... return g
....
>>partial(f, 1, 2)(3)
6
>>partial(f, 1)(2, 3)
6
>>partial(f)(1, 2, 3)
6

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

Peter
Jul 26 '06 #3
http://cheeseshop.python.org/pypi/functional

learn lisp/scheme!
http://cs.wwc.edu/KU/PR/Scheme.html
Peter Otten wrote:
li******@gmail.com wrote:
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:
>def f(a, b, c):
... return a + b + c
...
>def partial(f, *args):
... def g(*more):
... return f(*args+more)
... return g
...
>partial(f, 1, 2)(3)
6
>partial(f, 1)(2, 3)
6
>partial(f)(1, 2, 3)
6

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

Peter
Jul 26 '06 #4

Peter Otten wrote:
li******@gmail.com wrote:
Bound methods are limited to one implicit parameter. What you need is
partial function application:
>def f(a, b, c):
... return a + b + c
...
>def partial(f, *args):
... def g(*more):
... return f(*args+more)
... return g
...
>partial(f, 1, 2)(3)
6
>partial(f, 1)(2, 3)
6
>partial(f)(1, 2, 3)
6

See http://www.python.org/dev/peps/pep-0309/ for more.
Thanks, this explanation is great. Nice to see also that python 2.5
will have it,

Jul 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin Smith | last post by:
I want to evaluate an expression using eval(), but I only want to supply the variable values "on demand." For example, let's say that I want to evaluate 'x+y'. Normally, I would create a...
5
by: Josh English | last post by:
I may not be using the language properly, but I suspect what I have is a binding problem and I'm wondering if there's a better solution than what I've come up with. I'm working on a Stack based...
1
by: Andy Wells | last post by:
I'm using VB.NET and I have an application that binds a schema to the main form's controls, and the user has the ability to load an XML file through the schema and into the bound form. My...
1
by: Mike Dole | last post by:
Why is it that when I want to use an attribute like for instance: .WriteAttributeString("WaiterId", "14") and later on (in another application) want to bind the xml to a datagrid with readxml I'm...
5
by: Pete | last post by:
Hi, I want to essentially perform late binding in c#. The rough equivalent in VB (6) would be: dim x as ifcMyInterface dim SensibleClassName as string SensibleClassName = .... 'something...
19
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this...
1
by: google | last post by:
I have created a simple HelloWorld application in WSE2.0 SP3. When I browse to the endpoint url over http (e.g. http://localhost/TestService.ashx) the WSDL is fine and I can create a client...
18
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the...
4
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, what is the difference between the late binding and reflection? clara -- thank you so much for your help
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.