Re: [Tutor] lambda

2013-01-25 Thread Alan Gauld
On 26/01/13 01:22, eryksun wrote: With a function call you no longer need the default parameter hack (i.e. x=i, y=j). You can make a closure over the local i and j in buttonFunMaker: def buttonFunMaker(i, j): def func(): return ttt(i, j) return func Good ca

Re: [Tutor] lambda

2013-01-25 Thread eryksun
On Fri, Jan 25, 2013 at 7:14 PM, Alan Gauld wrote: > > Your problem of course is that you need i and j to be dynamically defined so > you need to create and call a function that returns a function like this > > def buttonFunMaker(i,j): > def func(x=i,y=j): > return ttt(x,y) > retur

Re: [Tutor] lambda

2013-01-25 Thread anthonym
Thanks Alan. I prefer the lambda too. Especially given how much code I saved. I forgot about i and j being dynamic and the call function. On 1/25/13 4:14 PM, "Alan Gauld" wrote: >On 25/01/13 23:57, anthonym wrote: > >> I don't think my classmates will understand the use of lambda here but I

Re: [Tutor] lambda

2013-01-25 Thread Danny Yoo
On Fri, Jan 25, 2013 at 4:57 PM, anthonym wrote: > I have the code below that I used to create a simple tic tac toe game for > class. I am learning Python but have programmed in C+ before so I brought > over a lambda and found that it worked in Python. Unfortunately I don't > think my classmate

Re: [Tutor] lambda

2013-01-25 Thread Alan Gauld
On 25/01/13 23:57, anthonym wrote: I don't think my classmates will understand the use of lambda here but I am having are hard time converting that to strictly python. lambdas are strictly python but they can be easily reanslated into a named function as lambda p: expr becomes def f(p):

Re: [Tutor] Lambda?? Whaaaaat?

2012-08-31 Thread Alan Gauld
On 31/08/12 05:43, Steven D'Aprano wrote: Where does the name come from? Lambda is the Greek letter L, and for reasons I don't know, it is the traditional name used for functions in some of the more abstract areas of computer science. More specifically it is the name of a branch of mathematics

Re: [Tutor] Lambda?? Whaaaaat?

2012-08-30 Thread Steven D'Aprano
On 31/08/12 13:39, Scurvy Scott wrote: I'm fairly new to python having recently completed LPTHW. While randomly reading stack overflow I've run into "lambda" but haven't seen an explanation of what that is, how it works, etc. Would anyone care to point me in the right direction? Lambda is just

Re: [Tutor] Lambda?? Whaaaaat?

2012-08-30 Thread Dave Angel
On 08/30/2012 11:39 PM, Scurvy Scott wrote: > I'm fairly new to python having recently completed LPTHW. While randomly > reading stack overflow I've run into "lambda" but haven't seen an explanation > of what that is, how it works, etc. > Would anyone care to point me in the right direction? lam

Re: [Tutor] Lambda?? Whaaaaat?

2012-08-30 Thread Dwight Hutto
Hey Scott, Always refer to google and the python docs first. from http://docs.python.org/tutorial/controlflow.html#lambda-forms 4.7.5. Lambda Forms By popular demand, a few features commonly found in functional programming languages like Lisp have been added to Python. With the lambda

Re: [Tutor] lambda

2011-03-20 Thread Ajit Deshpande
Fantastic explanation everyone. Thanks a lot. Looking forward to using lambda going forward. ~ Ajit Deshpande On Sat, Mar 19, 2011 at 6:07 PM, Steven D'Aprano wrote: > Ajit Deshpande wrote: > >> I am trying to figure out where lambda functions can be useful. Has anyone >> used them in real worl

Re: [Tutor] lambda

2011-03-19 Thread Steven D'Aprano
Ajit Deshpande wrote: I am trying to figure out where lambda functions can be useful. Has anyone used them in real world? Of course. lambdas are especially useful for callback functions, which are especially common when doing GUI programming. From my reading so far, I hear people claim tha

Re: [Tutor] lambda

2011-03-19 Thread Alan Gauld
"Ajit Deshpande" wrote I am trying to figure out where lambda functions can be useful. Has anyone used them in real world? Yers lots of people, all over the place. They are very useful things. Why would anyone use a one liner anonymous function, To pass as an argument to another functio

Re: [Tutor] lambda

2011-03-19 Thread Dave Angel
On 01/-10/-28163 02:59 PM, Ajit Deshpande wrote: I am trying to figure out where lambda functions can be useful. Has anyone used them in real world? From my reading so far, I hear people claim that lambda can be a useful replacement for small functions. Most examples didn't make much sense to

Re: [Tutor] lambda

2011-03-19 Thread bob gailer
In my Python Pipelines program I have the following opts = { 'characters' : (lambda rec, tot: tot + len(rec), 0), 'words' : (lambda rec, tot: tot + len(rec.split()), 0), 'lines' : (lambda rec, tot: tot + 1, 0), 'minline' :(lambda rec, tot: min(len(rec), tot), 9

Re: [Tutor] lambda

2011-03-19 Thread bob gailer
On 3/19/2011 9:44 AM, Ajit Deshpande wrote: I am trying to figure out where lambda functions can be useful. Has anyone used them in real world? From my reading so far, I hear people claim that lambda can be a useful replacement for small functions. Most examples didn't make much sense to me.

Re: [Tutor] lambda

2011-03-19 Thread Adam Bark
On 19/03/11 14:44, Ajit Deshpande wrote: I am trying to figure out where lambda functions can be useful. Has anyone used them in real world? From my reading so far, I hear people claim that lambda can be a useful replacement for small functions. Most examples didn't make much sense to me. Wh

Re: [Tutor] lambda in python

2010-11-27 Thread Alan Gauld
"john tsolox" wrote seeing various examples of lambda in python being ALL one-liners. These one-liners inside a lambda seems not to have the full permissible use of the full power of python language (if,for,while). Is this correct? The lambda expression was introduced into Python by popula

Re: [Tutor] lambda in python

2010-11-26 Thread Steven D'Aprano
john tsolox wrote: since in Java i can pass an anonymous class to a function, and this anon class has member functions that can contain a body of implementation codes having the full expression of permissible syntax (if,for,while...), my question is, after seeing various examples of lambda in p

Re: [Tutor] Lambda function, was: Simple counter to determine frequencies of words in adocument

2010-11-24 Thread ALAN GAULD
> > Notice that it uses key as a function inside sorted. > > And lambda creates a function so sorted applies > > the lambda to each item in turn to retrieve the sort key. > > OK. I get it. Thanks a lot. This should have been clear if I had been > able to see the code for the sorted() method.

Re: [Tutor] Lambda function, was: Simple counter to determine frequencies of words in adocument

2010-11-23 Thread ALAN GAULD
> OK, all of this is perfectly clear but what is still difficult for me > to understand is the following. If you remember the line of code in > question was: > > >sorted(word_table.items(), key=lambda item: item[1], reverse=True) > > This is supposed to return a list of tuples such as > >

Re: [Tutor] Lambda function, was: Simple counter to determine frequencies of words in adocument

2010-11-22 Thread ALAN GAULD
> What I don't understand is the nature of the term 'item'. Is it a > variable? Yes, its just a descriptive name. You could have used x just as easily, Python doesn't know, nor care. It is just like in defining any normal function def f(x): return x is exactly the same as: def f(data): ret

Re: [Tutor] Lambda function, was: Simple counter to determine frequencies of words in adocument

2010-11-22 Thread Josep M. Fontana
Thanks Alan and Emile, >> By the way, I know what a lambda function is and I read about the key >> parameter in sorted() but I don't understand very well what >> "key=lambda item: item[1]" does. ... >> What I don't understand is the syntax of "item : item[1]". > ... Alan says: > So reverse eng

Re: [Tutor] lambda vs list comp

2010-07-27 Thread Steven D'Aprano
On Wed, 28 Jul 2010 02:44:02 am Jon Crump wrote: > Just as a matter of curiosity piqued by having to understand someone > else's code. Is the difference here just a matter of style, or is one > better somehow than the other? Remember that list comprehensions didn't exist until a few years ago, so

Re: [Tutor] lambda vs list comp

2010-07-27 Thread Rich Lovely
On 27 July 2010 17:53, Mac Ryan wrote: > On Tue, 2010-07-27 at 09:44 -0700, Jon Crump wrote: >> Just as a matter of curiosity piqued by having to understand someone >> else's code. Is the difference here just a matter of style, or is one >> better somehow than the other? >> >> >>> l >> [0, 1, 2, 3

Re: [Tutor] lambda vs list comp

2010-07-27 Thread Mac Ryan
On Tue, 2010-07-27 at 09:44 -0700, Jon Crump wrote: > Just as a matter of curiosity piqued by having to understand someone > else's code. Is the difference here just a matter of style, or is one > better somehow than the other? > > >>> l > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > > >>> ','.join([str(

Re: [Tutor] lambda: print('x') raises SyntaxError?

2007-07-06 Thread wcyee
Ahh. It seems so obvious now. :) Thanks, Wesley & Kent! On 7/5/07, wesley chun <[EMAIL PROTECTED]> wrote: On 7/5/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > wc yeee wrote: > > Hi. Is there a reason the code below raises a syntax error? It's > > probably something silly on my part, but I can

Re: [Tutor] lambda: print('x') raises SyntaxError?

2007-07-05 Thread wesley chun
On 7/5/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > wc yeee wrote: > > Hi. Is there a reason the code below raises a syntax error? It's > > probably something silly on my part, but I can't figure it out: > > > > > > >>> b = lambda: print('bar') > > File "", line 1 > > b = lambda: print('bar

Re: [Tutor] lambda: print('x') raises SyntaxError?

2007-07-05 Thread Kent Johnson
wc yeee wrote: > Hi. Is there a reason the code below raises a syntax error? It's > probably something silly on my part, but I can't figure it out: > > > >>> b = lambda: print('bar') > File "", line 1 > b = lambda: print('bar') > ^ > SyntaxError: invalid synta

Re: [Tutor] lambda and creating GUIs

2006-06-17 Thread Alan Gauld
>I understand this: >> >> def f(w): gtk.main_quit() >> button.connect("clicked", f) >> > > Now my question is what is "w"? What is being passed > to the function? That will be defined by GTk. The framework will define what each of its event hamdler callback functions looks like in terms of p

Re: [Tutor] lambda and creating GUIs

2006-06-16 Thread John Fouhy
On 17/06/06, Christopher Spears <[EMAIL PROTECTED]> wrote: > I understand this: > > > > def f(w): gtk.main_quit() > > button.connect("clicked", f) > > > Now my question is what is "w"? What is being passed > to the function? I don't know GTK, but I would guess some kind of event class. Other GUI

Re: [Tutor] lambda and creating GUIs

2006-06-16 Thread Christopher Spears
I understand this: > > def f(w): gtk.main_quit() > button.connect("clicked", f) > > lambda simply saves cluttering up the code with lots > of tiny function > derfinitions which are never referred to apart from > in the binding > operation. > Now my question is what is "w"? What is being passe

Re: [Tutor] lambda and creating GUIs

2006-06-16 Thread Alan Gauld
>I have been reading though the PyGTK tutorial. > Can anyone explain how lambda is being used in this > statement: > > button.connect("clicked", lambda w: gtk.main_quit()) lambda is being used to create an anonymous function. The code could be rewritten like this: def f(w): gtk.main_quit() butto

Re: [Tutor] lambda and creating GUIs

2006-06-15 Thread John Fouhy
(resending to include Tutor -- sorry for the dupe, Christopher) On 16/06/06, Christopher Spears <[EMAIL PROTECTED]> wrote: > I have been reading though the PyGTK tutorial. > Can anyone explain how lambda is being used in this > statement: > > button.connect("clicked", lambda w: gtk.main_quit())

Re: [Tutor] lambda in a loop

2005-11-17 Thread Christian Wyglendowski
Fred said: > >> Obviously, the lambda is using "value" at the end of the loop (4), > >>rather than what I want, "value" during the loop (0,1,2,3). Christian said: > > Right. I think the issue is that your lambda calls another funtion. > > However, the function isn't called until the lambda is c

Re: [Tutor] lambda in a loop

2005-11-16 Thread Danny Yoo
> Just to be able to talk about things, let's give a name to the global > namespace as: "G". > > Whenever we call a function, we build a new environment that's chained up > to the one we're in at the time of function construction. This > corresponds to what people's ideas of the "stack frame" is.

Re: [Tutor] lambda in a loop

2005-11-16 Thread Danny Yoo
> The original solution does use a closure. The problem is that variables > are not bound into a closure until the scope of the variable exits. That > is why using a separate factory function works - the closure is bound > when the factory function exits which happens each time through the > loop.

Re: [Tutor] lambda in a loop

2005-11-16 Thread Kent Johnson
Christian Wyglendowski wrote: >>-Original Message- >>From: [EMAIL PROTECTED] >> If I have this code: >> Obviously, the lambda is using "value" at the end of the loop (4), >>rather than what I want, "value" during the loop (0,1,2,3). > > Right. I think the issue is that your lambda ca

Re: [Tutor] lambda in a loop

2005-11-16 Thread Alan Gauld
def doLambda(val): print "value 2:", val commands = [] for value in range(5): print "value 1:", value commands.append(lambda:doLambda(value)) Close but not quite. Try: commands.append(lambda v=value:doLambda(v)) value is a local variable in doLambda

Re: [Tutor] lambda in a loop

2005-11-16 Thread Danny Yoo
> > def doLambda(val): >print "value 2:", val > > commands = [] > for value in range(5): >print "value 1:", value >commands.append(lambda:doLambda(value)) > > for c in commands: >c() Hi Fred, Ah, this one of those unfrequently asked questions.

Re: [Tutor] lambda in a loop

2005-11-16 Thread Christian Wyglendowski
> -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Fred Lionetti > Sent: Wednesday, November 16, 2005 2:32 PM > To: tutor@python.org > Subject: [Tutor] lambda in a loop > > Hi everyone, Hello, > If I have this code: > > ---