Python newbie needs constructive suggestions

D

davew-python

What is the idiomatically appropriate Python way to pass, as a "function-type parameter", code that is most clearly written with a local variable?

For example, map takes a function-type parameter:

map(lambda x: x+1, [5, 17, 49.5])

What if, instead of just having x+1, I want an expression that is most clearly coded with a variable that is needed _only_ inside the lambda, e.g. if I wanted to use the name "one" instead of 1:

map(lambda x: (one = 1 x+one), [5, 17, 49.5])

This sort of thing is of course straightforward in many other languages with anonymous functions (Scheme, Haskell, Smalltalk, etc), and I saw in the archives the discussion from 2003 about "How do I get Scheme-like let bindings in Python". Many of the answers seem to boil down to "You should not write Scheme programs in Python, you should write Python programs in Python". As someone new to Python, I'd like a little more detail on this, so I'm asking _what_ I should do when I want to pass, to something like map, code that is most clearly expressed with a local variable?

Do I

a) give up on using a local variable and just substitute the value in its place (going back to my original use of map),

b) give up on using an anonymous function and create a named "successor" function with "def",

c) give up on making "one" local to the lambda and create it in the scope in which I'm calling map, even if I don't otherwise need it there,

d) give up on using Python and go back to Scheme, or

e) do something else clever and Pythonic that I don't know about yet?

What is the idiomatically correct way to do this in Python?

Thanks for any constructive advice you can give,
Dave Wonnacott


P.S., For those who want a bit more context about what I'm doing, I'll provide it -- anyone else is welcome to skip the rest of this message.


As you may guess, I am in the process of learning Python. I have some experience with C/C++ and Scheme and other forms of Lisp, as well as passing familiarity with a bunch of other languages.

I teach an introductory CS course in which I cover a variety of topics that I believe are central to computer science and can be expressed in any language, such as algorithm design, unit and integration testing, writing code that maintains an invariant property of a set of variables. However, I do not like the "language-free" or "all-pseudocode" approach to teaching -- I believe all examples should be shown in a real language, and that the code should be as true to the customary idioms of that language as possible. This limits my choice of language somewhat, since I include elements of functional programming, imperative programming, and object-oriented programming -- I'm not happy doing things like cramming functions into unnecessary classes in Java. However, I currently believe that Python will be at least as good as C++ for what I want to do, and am exploring the details of how it would work out.

One of the things I'm doing is discussing various styles of thinking about the execution of an algorithm, specifically as a process of textual substitution of equals for equals (as one would generally do in Haskell or other functional languages, or in mathematics) or a sequence of ordered steps to be followed (as one would generally do in imperative languages). Note that, while Haskell programmers may say that substitution is the true way that their programs are executed, and C++ programmers may say that a sequence of ordered steps is what's "really going on", the actual process of going from your source code to the output of your program can be blending of these two techniques -- for example, a C++ compiler may perform substitution where it is legal to do so, producing a machine language program that is executed _mostly_ in order (though a processor may commute the order of execution where it is legal to do so). Anyway, in almost any language, there are _some_ ways to perfo!
rm substitutions without changing the result of a piece of code (e.g. substituting the value of a variable that is only assigned once, for each of its uses), and some things that can be visualized in terms of execution of steps even if one wishes to do so (even in a language like Haskell).

The question of the use of variables inside a lambda is relevant to my understanding of the contexts in which one can think of Python programs in terms of substitution, as well as my learning of proper Python idioms.
 
F

faulkner

optional arguments.
map(lambda x, one=1: x + one, ...)

it is entirely possible, however, to implement let in python.
def let(**kw):
sys._getframe(2).f_locals.update(kw)

def begin(*a):
return a[-1]

map(lambda x: begin(let(one=1), x+one), range(10))

i really should warn you, though, that most pythoneers will cringe at
code like that, no matter how well they understand it. write python in
python, and you'll have more friends.


What is the idiomatically appropriate Python way to pass, as a "function-type parameter", code that is most clearly written with a local variable?

For example, map takes a function-type parameter:

map(lambda x: x+1, [5, 17, 49.5])

What if, instead of just having x+1, I want an expression that is most clearly coded with a variable that is needed _only_ inside the lambda, e.g. if I wanted to use the name "one" instead of 1:

map(lambda x: (one = 1 x+one), [5, 17, 49.5])

This sort of thing is of course straightforward in many other languages with anonymous functions (Scheme, Haskell, Smalltalk, etc), and I saw in the archives the discussion from 2003 about "How do I get Scheme-like let bindings in Python". Many of the answers seem to boil down to "You should not write Scheme programs in Python, you should write Python programs in Python". As someone new to Python, I'd like a little more detail on this, so I'm asking _what_ I should do when I want to pass, to something like map, code that is most clearly expressed with a local variable?

Do I

a) give up on using a local variable and just substitute the value in its place (going back to my original use of map),

b) give up on using an anonymous function and create a named "successor" function with "def",

c) give up on making "one" local to the lambda and create it in the scope in which I'm calling map, even if I don't otherwise need it there,

d) give up on using Python and go back to Scheme, or

e) do something else clever and Pythonic that I don't know about yet?

What is the idiomatically correct way to do this in Python?

Thanks for any constructive advice you can give,
Dave Wonnacott


P.S., For those who want a bit more context about what I'm doing, I'll provide it -- anyone else is welcome to skip the rest of this message.


As you may guess, I am in the process of learning Python. I have some experience with C/C++ and Scheme and other forms of Lisp, as well as passing familiarity with a bunch of other languages.

I teach an introductory CS course in which I cover a variety of topics that I believe are central to computer science and can be expressed in any language, such as algorithm design, unit and integration testing, writing code that maintains an invariant property of a set of variables. However, I do not like the "language-free" or "all-pseudocode" approach to teaching -- I believe all examples should be shown in a real language, and that the code should be as true to the customary idioms of that language as possible. This limits my choice of language somewhat, since I include elements of functional programming, imperative programming, and object-oriented programming -- I'm not happy doing things like cramming functions into unnecessary classes in Java. However, I currently believe that Python will be at least as good as C++ for what I want to do, and am exploring the details of how it would work out.

One of the things I'm doing is discussing various styles of thinking about the execution of an algorithm, specifically as a process of textual substitution of equals for equals (as one would generally do in Haskell or other functional languages, or in mathematics) or a sequence of ordered steps to be followed (as one would generally do in imperative languages). Note that, while Haskell programmers may say that substitution is the true way that their programs are executed, and C++ programmers may say that a sequence of ordered steps is what's "really going on", the actual process of going from your source code to the output of your program can be blending of these two techniques -- for example, a C++ compiler may perform substitution where it is legal to do so, producing a machine language program that is executed _mostly_ in order (though a processor may commute the order of execution where it is legal to do so). Anyway, in almost any language, there are _some_ ways to perfo!
rm substitutions without changing the result of a piece of code (e.g. substituting the value of a variable that is only assigned once, for each of its uses), and some things that can be visualized in terms of execution of steps even if one wishes to do so (even in a language like Haskell).

The question of the use of variables inside a lambda is relevant to my understanding of the contexts in which one can think of Python programs in terms of substitution, as well as my learning of proper Python idioms.
 
J

Justin Azoff

What is the idiomatically appropriate Python way to pass, as a "function-type parameter", code that is most clearly written with a local variable?

For example, map takes a function-type parameter:

map(lambda x: x+1, [5, 17, 49.5])

What if, instead of just having x+1, I want an expression that is most clearly coded with a variable that is needed _only_ inside the lambda, e.g. if I wanted to use the name "one" instead of 1:

map(lambda x: (one = 1 x+one), [5, 17, 49.5])

I believe most people would just write something like this:

def something():
#local helper function to add one to a number
def addone(x):
one = 1
return x+one
return map(addone, [5, 17, 49.5])
 
L

Lawrence D'Oliveiro

In message <[email protected]>,
b) give up on using an anonymous function and create a named "successor"
function with "def",

This is what you have to do. For some reason mr van Rossum has this aversion
to anonymous functions, and tries to cripple them as much as possible.
 
D

Dennis Lee Bieber

What is the idiomatically appropriate Python way to pass, as a "function-type parameter", code that is most clearly written with a local variable?

For example, map takes a function-type parameter:

map(lambda x: x+1, [5, 17, 49.5])

What if, instead of just having x+1, I want an expression that is most clearly coded with a variable that is needed _only_ inside the lambda, e.g. if I wanted to use the name "one" instead of 1:

map(lambda x: (one = 1 x+one), [5, 17, 49.5])
map(lambda x, one=1, two=2: x * two + one, [5, 17, 49.5])

Does that do what you want?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Peter Otten

What is the idiomatically appropriate Python way to pass, as a
"function-type parameter", code that is most clearly written with a local
variable?

For example, map takes a function-type parameter:

map(lambda x: x+1, [5, 17, 49.5])

What if, instead of just having x+1, I want an expression that is most
clearly coded with a variable that is needed only inside the lambda, e.g.
if I wanted to use the name "one" instead of 1:

map(lambda x: (one = 1  x+one), [5, 17, 49.5])

For a simple constant expression, your example is how it's done most
frequently. When it gets more complicated the classic trick is to use a
default argument:
map(lambda x, delta=2**10: x+delta, [5, 17, 49.5])
[1029, 1041, 1073.5]

A closure works equally well:
.... def add(x):
.... return x + delta
.... return add
....
map(make_adder(42), [5, 17, 49.5])
[47, 59, 91.5]

So does a callable class...
.... def __init__(self, delta):
.... self.delta = delta
.... def __call__(self, x):
.... return x + self.delta
....
[326, 338, 370.5]

which has the advantage that it can maintain state between calls (e. g. if
you were to build an accumulator it would be your only clean option).

In the real world the first two are most common, but they are only used when
the variable is not for immediate consumption:

# wrong
adders = [lambda x: x + delta for delta in range(3)]
[a(0) for a in adders]
[2, 2, 2]

# fixed
adders = [lambda x, delta=delta: x + delta for delta in range(3)]
[a(0) for a in adders]
[0, 1, 2]
Do I

  d) give up on using Python and go back to Scheme, or

If you stick with Python your students can soon start to experiment with
concepts and algorithms, and at the same time you provide them with a tool
that may be useful in their daily work (think scipy).

Peter
 
D

David G. Wonnacott

Many thanks to those of you who responded to my question about
anonymous functions with local variables, filling me in on

e) do something else clever and Pythonic that I don't know about yet?

by pointing out that I can use (among other good things) lambda with
default arguments. That should suit my immediate needs well, since I
don't need to maintain state (or avoid maintaning state); I may also
look into closures (possibly anonymous ones?) and callable classes if
I need to go beyond this. And, of course, if the code is complex
enough, then one should give up the anonymous function and name it.

Rest assured that I will _not_ be attempting to write "let" in Python
and use it.

It looks like Python may be every much as big an improvement over C++
as I had hoped.

Dave Wonnacott
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
What is the idiomatically appropriate Python way to pass, as a
"function-type parameter", code that is most clearly written with a
local variable?

def functionWithLocal(andArg):
localVar = 42
return andArg+localVar

map(functionWithLocal, range(42))

For example, map takes a function-type parameter:

map(lambda x: x+1, [5, 17, 49.5])

What if, instead of just having x+1, I want an expression that is
most clearly coded with a variable that is needed _only_ inside the
lambda, e.g. if I wanted to use the name "one" instead of 1:

map(lambda x: (one = 1 x+one), [5, 17, 49.5])

map(lambda x, one=42 : x + one, [5, 17, 49.5])
This sort of thing is of course straightforward in many other
languages with anonymous functions (Scheme, Haskell, Smalltalk, etc),
and I saw in the archives the discussion from 2003 about "How do I
get Scheme-like let bindings in Python". Many of the answers seem to
boil down to "You should not write Scheme programs in Python, you
should write Python programs in Python".

A very sensible advice IMHO. Python - while having some support for FP -
is still statement-based and mostly on the OO/procedural side.

(snip)
 
B

Bruno Desthuilliers

Lawrence D'Oliveiro a écrit :
In message <[email protected]>,



This is what you have to do.

Not necessarily.

map(lambda x, one=1: one + x, range(42))
For some reason mr van Rossum has this aversion
to anonymous functions, and tries to cripple them as much as possible.

For some reasons, including the fact that Python is statement based and
have significative indentation, it's actually not trivial to come with a
better syntax for lambdas. The horse has been beaten to hell and back,
and no one managed to propose anything worth implementing so far. But if
you have the solution to this problem, please share...
 
C

Chris Lambacher

Lawrence D'Oliveiro a ?crit :

Not necessarily.

map(lambda x, one=1: one + x, range(42))
Note that in Python this particular expression is normally written as:
a = [x + 1 for x in range(42)]

List comprehension and Generator expressions are generally seen as the
solution to not having to deal with map, and reduce.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top