Re: lambda closure question

2005-02-22 Thread Karl Anderson
Ted Lilley [EMAIL PROTECTED] writes: What I want to do is pre-load functions with arguments by iterating through a list like so: class myclass: ...pass def func(self, arg): ...print arg mylist = [my, sample, list] for item in mylist: ...setattr(myclass, item, lamdba self:

Re: lambda closure question

2005-02-21 Thread Carl Banks
Mike Meyer wrote: Carl Banks [EMAIL PROTECTED] writes: Say you have a suite of functions, all of which are called by some main function and each other, and all of which need to access a lot of the same data. The best, most straightforward way to do it is to have the common data be a

Re: lambda closure question

2005-02-21 Thread Carl Banks
jfj wrote: The costly extra feature is this: ### def foo(): def f(): print x x=1 f() x=2 f() return f foo()() # which prints '1 2 2' The fractal code runs a little _slower_ because of this ability. Although the

Re: lambda closure question

2005-02-21 Thread Antoon Pardon
Op 2005-02-19, jfj schreef [EMAIL PROTECTED]: Carl Banks wrote: Ted Lilley wrote: Unfortunately, it doesn't work. It seems the closure keeps track of the variable fed to it dynamically - if the variable changes after [...] At least, that's the explanation I'm deducing from this behavior.

Re: lambda closure question

2005-02-21 Thread jfj
Antoon Pardon wrote: Op 2005-02-19, jfj schreef [EMAIL PROTECTED]: once foo() returns there is no way to modify 'x'! It becomes a kind of constant. In this particular case yes. But not in general, what about this: def F(): ... l = [] ... def pop(): ... return l.pop() ... def push(e):

Re: lambda closure question

2005-02-21 Thread Antoon Pardon
Op 2005-02-21, jfj schreef [EMAIL PROTECTED]: Antoon Pardon wrote: Op 2005-02-19, jfj schreef [EMAIL PROTECTED]: once foo() returns there is no way to modify 'x'! It becomes a kind of constant. In this particular case yes. But not in general, what about this: def F(): ... l = []

Re: lambda closure question

2005-02-21 Thread Diez B. Roggisch
But I'll get back at what seems you actually wanted to say: That there is no way to rebind 'x' or in my case 'l' and with that I have to agree although I personnaly find that a lack in python It's not only that way in python, but in java too. So it seems that there is a fundamental principle

Re: lambda closure question

2005-02-21 Thread Duncan Booth
Antoon Pardon wrote: But I'll get back at what seems you actually wanted to say: That there is no way to rebind 'x' or in my case 'l' and with that I have to agree although I personnaly find that a lack in python 'no way' is a bit strong. You can use hacks such as the one I posted a couple

Re: lambda closure question

2005-02-21 Thread Antoon Pardon
Op 2005-02-21, Diez B. Roggisch schreef [EMAIL PROTECTED]: But I'll get back at what seems you actually wanted to say: That there is no way to rebind 'x' or in my case 'l' and with that I have to agree although I personnaly find that a lack in python It's not only that way in python, but in

Re: lambda closure question

2005-02-21 Thread Paul Rubin
Diez B. Roggisch [EMAIL PROTECTED] writes: It's not only that way in python, but in java too. So it seems that there is a fundamental principle behind it: In a language that allows sideeffects, these will actually happen. Can you even have nested functions in Java? Algol-60 did things the way

Re: lambda closure question

2005-02-21 Thread Diez B. Roggisch
Paul Rubin wrote: Diez B. Roggisch [EMAIL PROTECTED] writes: It's not only that way in python, but in java too. So it seems that there is a fundamental principle behind it: In a language that allows sideeffects, these will actually happen. Can you even have nested functions in Java?

Re: lambda closure question

2005-02-21 Thread jfj
Carl Banks wrote: transformations gets rebound, so you'd need a reference to it. That certainly is an application. I guess it depends on one's programming background. I'd only use nested (function, class) definition to accomplish such a feature: def genclass(x,y):

[OT] Re: lambda closure question

2005-02-21 Thread Steven Bethard
Antoon Pardon wrote: def F(): ... l = [] ... def pop(): ... return l.pop() ... def push(e): ... l.append(e) ... return pop, push ... Just a side note to point out that another way of writing this is: py def F(): ... l = [] ... return l.pop, l.append ... You'll get the same

Re: lambda closure question

2005-02-20 Thread Carl Banks
jfj wrote: Yes, but according to the python philosophy one could pass locals() to the nested function and grab the values from there. Seems practical for the rareness of this... First of all, I really don't agree that it's a rare need. I use it pretty often. Second of all, the whole point

Re: lambda closure question

2005-02-20 Thread jfj
Carl Banks wrote: Say you want to calculate a list of points of an iterated fractal. Here's the idea: you have a set of linear transformations. You take the origin (0,0) as the first point, and then apply each transformation in turn to get a new point. You recursively apply each transformation

Re: lambda closure question

2005-02-20 Thread Dirk Thierbach
Ted Lilley [EMAIL PROTECTED] wrote: As a side note, I'm familiar with the term currying from a friend who learned ML and Scheme quite some time ago. Not sure if that's the true origin, but it was a sufficiently different context from Python (or at least I thought) that I didn't want to rely

Re: lambda closure question

2005-02-20 Thread Mike Meyer
Carl Banks [EMAIL PROTECTED] writes: Say you have a suite of functions, all of which are called by some main function and each other, and all of which need to access a lot of the same data. The best, most straightforward way to do it is to have the common data be a local variable of the main

Re: lambda closure question

2005-02-19 Thread Carl Banks
Ted Lilley wrote: What I want to do is pre-load functions with arguments by iterating through a list like so: class myclass: ...pass def func(self, arg): ...print arg mylist = [my, sample, list] for item in mylist: ...setattr(myclass, item, lamdba self: func(self, item))

Re: lambda closure question

2005-02-19 Thread jfj
Carl Banks wrote: Ted Lilley wrote: Unfortunately, it doesn't work. It seems the closure keeps track of the variable fed to it dynamically - if the variable changes after [...] At least, that's the explanation I'm deducing from this behavior. And that's the correct explanation, chief. It is

Re: lambda closure question

2005-02-19 Thread Carl Banks
jfj wrote: Carl Banks wrote: Ted Lilley wrote: Unfortunately, it doesn't work. It seems the closure keeps track of the variable fed to it dynamically - if the variable changes after [...] At least, that's the explanation I'm deducing from this behavior. And that's the

Re: lambda closure question

2005-02-19 Thread jfj
Carl Banks wrote: jfj wrote: Carl Banks wrote: Ted Lilley wrote: Unfortunately, it doesn't work. It seems the closure keeps track of the variable fed to it dynamically - if the variable changes after [...] At least, that's the explanation I'm deducing from this behavior. And that's the correct

Re: lambda closure question

2005-02-19 Thread Kent Johnson
Ted Lilley wrote: What I want to do is pre-load functions with arguments by iterating through a list like so: class myclass: ...pass def func(self, arg): ...print arg mylist = [my, sample, list] for item in mylist: ...setattr(myclass, item, lamdba self: func(self, item)) This attaches

Re: lambda closure question

2005-02-19 Thread Steven Bethard
Carl Banks wrote: You may not be aware of it, but what you're trying to do is called currying; you might want to search the Python Cookbook for recipes on it. Or look for partial function application which has been argued to be the correct term for this use... Also see PEP 309:

Re: lambda closure question

2005-02-19 Thread Ted Lilley
Wow, a lot of great discussion. Almost a bit too much for me to grasp...I do see two or more nuggets that really address my issue. As a side note, I'm familiar with the term currying from a friend who learned ML and Scheme quite some time ago. Not sure if that's the true origin, but it was a

Re: lambda closure question

2005-02-19 Thread Ted Lilley
I replied a few minutes ago thanking everyone for their pointers. Very interesting reading. I don't see my post yet, so I do hope it comes through. I'm using a new newsreading service and don't yet have confidence in it. In any case, the addition of the default-setting argument in the lambda