Re: Python newbie needs constructive suggestions

2006-07-24 Thread Chris Lambacher
On Sat, Jul 22, 2006 at 09:31:26PM +0200, Bruno Desthuilliers wrote:
 Lawrence D'Oliveiro a ?crit :
  In message [EMAIL PROTECTED],
  [EMAIL PROTECTED] wrote:
  
  
   b) give up on using an anonymous function and create a named successor
   function with def,
  
  
  This is what you have to do.
 
 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.
 
  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...
 -- 
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


P.S. Re: Python newbie needs constructive suggestions

2006-07-24 Thread David G. Wonnacott
In response to my question, ``What is the idiomatically appropriate
Python way to pass, as a function-type parameter, code that is most
clearly written with a local variable?'', a number of you made very
helpful suggestions, including the use of a default argument; if one
wanted to give a name to the constant one in my original example,

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

one would write

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

I've been using this happily in several cases, but just discovered
that (of course), the variable x is not in scope until the
expression in the lambda, so if one wanted to, say, have a variable
that was 3*x, one could NOT write

   map(lambda x, threex=3*x: x+threex, [5, 17, 49.5])

[obviously there are easier ways to find 4*x, but I'm trying to keep
close to my simplified example].

I'll need to show the code to beginners, so I don't want to get into
the complexity of using the more advanced solutions that were
suggested (closures or callable classes), so I'm going to bail out in
such cases and just not use an anonymous function here.

I'm not sure there's really a question here, other than the implicit
please let me know if there's yet another cool thing I can do here,
but I thought I'd add this postscript to my earlier postings for the
benefit of anyone following this thread. [I often see interesting
threads in the archive but am left with an unresolved question I
wonder how that worked out].

Dave Wonnacott

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: P.S. Re: Python newbie needs constructive suggestions

2006-07-24 Thread Duncan Booth
David G. Wonnacott wrote:

 In response to my question, ``What is the idiomatically appropriate
 Python way to pass, as a function-type parameter, code that is most
 clearly written with a local variable?'', a number of you made very
 helpful suggestions, including the use of a default argument; if one
 wanted to give a name to the constant one in my original example,
 
map(lambda x: x+1, [5, 17, 49.5])
 
 one would write
 
map(lambda x, one=1: x+one, [5, 17, 49.5])
 
 I've been using this happily in several cases, but just discovered
 that (of course), the variable x is not in scope until the
 expression in the lambda, so if one wanted to, say, have a variable
 that was 3*x, one could NOT write
 
map(lambda x, threex=3*x: x+threex, [5, 17, 49.5])
 
 [obviously there are easier ways to find 4*x, but I'm trying to keep
 close to my simplified example].
 
 I'll need to show the code to beginners, so I don't want to get into
 the complexity of using the more advanced solutions that were
 suggested (closures or callable classes), so I'm going to bail out in
 such cases and just not use an anonymous function here.
 

Not using an anonymous function certainly sounds to me like the most 
idiomatically appropriate Python way to do this.

def fourx(x):
   threex = 3 * x
   return x + threex

map(fourx, [5, 17, 49.5])

The advantages over the lambda include the ability to use local variables 
and split the function naturally over multiple lines. Also you get to use a 
meaningful name to describe the function.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie needs constructive suggestions

2006-07-22 Thread Peter Otten
[EMAIL PROTECTED] wrote:

 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 make_adder(delta):
... 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...

 class Adder(object):
... def __init__(self, delta):
... self.delta = delta
... def __call__(self, x):
... return x + self.delta
...
 map(Adder(321), [5, 17, 49.5])
[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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie needs constructive suggestions

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


Re: Python newbie needs constructive suggestions

2006-07-22 Thread Bruno Desthuilliers
[EMAIL PROTECTED] 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)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie needs constructive suggestions

2006-07-22 Thread Bruno Desthuilliers
Lawrence D'Oliveiro a écrit :
 In message [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:
 
 
  b) give up on using an anonymous function and create a named successor
  function with def,
 
 
 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...
-- 
http://mail.python.org/mailman/listinfo/python-list


Python newbie needs constructive suggestions

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


Re: Python newbie needs constructive suggestions

2006-07-21 Thread 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.


[EMAIL PROTECTED] wrote:
 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 per!
 fo!
  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, 

Re: Python newbie needs constructive suggestions

2006-07-21 Thread Justin Azoff
[EMAIL PROTECTED] wrote:
 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])

-- 
- Justin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie needs constructive suggestions

2006-07-21 Thread Lawrence D'Oliveiro
In message [EMAIL PROTECTED],
[EMAIL PROTECTED] wrote:

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