how to write function that returns function 
Author Message
 how to write function that returns function

Quote:

> I am not a python expert, and I'm hoping someone
> can tell me how in Python to write a function
> of one argument x that returns a function of one
> argument y that returns x+y.

It's bad form to ask for help on your homework problems, after all *YOU*
are the one who is supposed to learn something...

Quote:
> Here, in Scheme, is what I want to write:

> (define foo (x) (lambda (y) (+ x y)))

Well (+ x y) is spelled 'x + y' in Python.
(lambda (y) ...) is spelled 'lambda y: ...'
and (define foo (x) ...) is spelled

  def foo(x):
      ...

putting it all together you get:

  def foo(x):
      lambda y: x + y

very straight-forward. Now for some arcane matters.

If you're running Python 2.2+ you're done.
If you're running Python 2.1, you need to put 'from __future__ import
nested_scopes' at the top of your module.
If you're running Python 1.5.x, you need to change the lambda to:

  lambda y, x=x: x + y

the x=x creates a binding local to the lambda which was required for the
1.5.x versions.

[snip]

If Guido can deal with the spam he gets from posting here, so can you.

-- bjorn



Sun, 31 Oct 2004 07:18:17 GMT  
 how to write function that returns function


Quote:

>> I am not a Python expert, and I'm hoping someone
>> can tell me how in Python to write a function
>> of one argument x that returns a function of one
>> argument y that returns x+y.

>It's bad form to ask for help on your homework problems, after all *YOU*
>are the one who is supposed to learn something...

True, but personally I consider far more irritating this pedantic attitude
that assumes that _all_ questions must be homework assignments. This is
unfortunately common in comp.lang.scheme and comp.lang.lisp. Let's keep it
away from comp.lang.python.

FYI: http://www.bugbear.com/bio.html

-----------------------
Fernando Rodriguez



Mon, 01 Nov 2004 02:57:51 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. how to write function that returns function

2. function that returns function

3. F95: array-returning function as argument to another function

4. about functions that return functions.

5. question about writing to the return value of a system function using vpi_put_value()

6. VC++ calling fortran function and fortran function calling a c++ function

7. Calling functions from functions from functions ...

8. returning ARRAY structures from DLL-functions in Dyalog

9. returning values through functions calls

10. return multiple values from an awk function?

11. return more than one parameter from a function

12. returning values from a function in a shell script

 

 
Powered by phpBB® Forum Software