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.
--
http://mail.python.org/mailman/listinfo/python-list