Re: lambda in list comprehension acting funny

2012-08-15 Thread Robert Miles
On 7/11/2012 11:39 PM, Dennis Lee Bieber wrote: On 12 Jul 2012 03:53:03 GMT, Steven D'Aprano declaimed the following in gmane.comp.python.general: "ALU class"? Googling gives me no clue. Arithmetic/Logic Unit http://en.wikipedia.org/wiki/Arithmetic_logic_unit http://en.wikipedia.o

Re: lambda in list comprehension acting funny

2012-07-15 Thread Hans Mulder
On 15/07/12 10:44:09, Chris Angelico wrote: > On Sun, Jul 15, 2012 at 6:32 PM, Steven D'Aprano > wrote: >> At compile time, Python parses the source code and turns it into byte- >> code. Class and function definitions are executed at run time, the same >> as any other statement. > > Between the p

Re: lambda in list comprehension acting funny

2012-07-15 Thread Terry Reedy
On 7/15/2012 4:32 AM, Steven D'Aprano wrote: On Sun, 15 Jul 2012 10:49:48 +1000, Chris Angelico wrote: On Sun, Jul 15, 2012 at 9:29 AM, Steven D'Aprano wrote: Not necessarily *compile* time, but the distinction is between when the function is defined (which may at compile time, or it may be a

Re: lambda in list comprehension acting funny

2012-07-15 Thread Chris Angelico
On Sun, Jul 15, 2012 at 6:32 PM, Steven D'Aprano wrote: > At compile time, Python parses the source code and turns it into byte- > code. Class and function definitions are executed at run time, the same > as any other statement. Between the parse step and the 'def' execution, a code object is cre

Re: lambda in list comprehension acting funny

2012-07-15 Thread Steven D'Aprano
On Sun, 15 Jul 2012 10:49:48 +1000, Chris Angelico wrote: > On Sun, Jul 15, 2012 at 9:29 AM, Steven D'Aprano > wrote: >> Not necessarily *compile* time, but the distinction is between when the >> function is defined (which may at compile time, or it may be at run >> time) versus when the function

Re: lambda in list comprehension acting funny

2012-07-14 Thread Chris Angelico
On Sun, Jul 15, 2012 at 9:29 AM, Steven D'Aprano wrote: > Not necessarily *compile* time, but the distinction is between when the > function is defined (which may at compile time, or it may be at run time) > versus when the function is called. I'd treat the def/lambda statement as "compile time"

Re: lambda in list comprehension acting funny

2012-07-14 Thread Dan Stromberg
On Sat, Jul 14, 2012 at 4:29 PM, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote: > I don't remember whether it is Javascript or PHP that uses dynamic > binding, but whichever it is, it is generally considered to be a bad > idea, at least as the default or only behaviour. > > Bash is

Re: lambda in list comprehension acting funny

2012-07-14 Thread Steven D'Aprano
On Fri, 13 Jul 2012 12:54:02 -0600, Ian Kelly wrote: > On Fri, Jul 13, 2012 at 11:53 AM, Hans Mulder wrote: >> The function `function` refers to a variable `VERBOSE` that isn't >> local. In some programming langauages, the interpreter would then scan >> the call stack at run-time, looking for a

Re: lambda in list comprehension acting funny

2012-07-14 Thread 88888 Dihedral
Alister於 2012年7月12日星期四UTC+8下午5時44分15秒寫道: > On Wed, 11 Jul 2012 08:43:11 +0200, Daniel Fetchinson wrote: > > >> funcs = [ lambda x: x**i for i in range( 5 ) ] > >> print funcs[0]( 2 ) > >> print funcs[1]( 2 ) > >> print funcs[2]( 2 ) > >> > >> This gives me > >> > >> 16 16 16 > >> > >> When I was e

Re: lambda in list comprehension acting funny

2012-07-14 Thread Steven D'Aprano
On Fri, 13 Jul 2012 21:53:10 -0700, rusi wrote: > On Jul 14, 8:43 am, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> On Fri, 13 Jul 2012 19:31:24 -0700, rusi wrote: >> > Consider the following >> >> > def foo(x): >> >     i = 100 >> >     if x: >> >         j = [i for i in range(10)]

Re: lambda in list comprehension acting funny

2012-07-13 Thread rusi
On Jul 14, 8:43 am, Steven D'Aprano wrote: > On Fri, 13 Jul 2012 19:31:24 -0700, rusi wrote: > > Consider the following > > > def foo(x): > >     i = 100 > >     if x: > >         j = [i for i in range(10)] > >         return i > >     else: > >         return i > > A simpler example: > > def foo(

Re: lambda in list comprehension acting funny

2012-07-13 Thread Steven D'Aprano
On Fri, 13 Jul 2012 19:31:24 -0700, rusi wrote: > Consider the following > > def foo(x): > i = 100 > if x: > j = [i for i in range(10)] > return i > else: > return i A simpler example: def foo(): i = 100 j = [i for i in range(10)] return i In Pyt

Re: lambda in list comprehension acting funny

2012-07-13 Thread rusi
On Jul 13, 10:53 pm, Hans Mulder wrote: > If you add `global VERBOSE` to `caller`, then there is only one > variable named `VERBOSE` and what `function` does, depends on > the most recent assignment to that variable. > > If you remove your `global VERBOSE`, then there are two > variables by that n

Re: lambda in list comprehension acting funny

2012-07-13 Thread Hans Mulder
On 13/07/12 20:54:02, Ian Kelly wrote: > I've also seen the distinction described as "early" vs. "late" binding > on this list, but I'm not sure how precise that is -- I believe that > terminology more accurately describes whether method and attribute > names are looked up at compile-time or at run

Re: lambda in list comprehension acting funny

2012-07-13 Thread Ian Kelly
On Fri, Jul 13, 2012 at 11:53 AM, Hans Mulder wrote: > The function `function` refers to a variable `VERBOSE` that > isn't local. In some programming langauages, the interpreter > would then scan the call stack at run-time, looking for a scope > where that name is defined. It would find the loca

RE: lambda in list comprehension acting funny

2012-07-13 Thread Prasad, Ramit
> >> VERBOSE = True > >> > >> def function(arg): > >> if VERBOSE: > >>print("calling function with arg %r" % arg) > >> process(arg) > >> > >> def caller(): > >> VERBOSE = False > >> function(1) > >> > >> - > >> Python semantics: fu

Re: lambda in list comprehension acting funny

2012-07-13 Thread Hans Mulder
On 13/07/12 18:12:40, Prasad, Ramit wrote: >> VERBOSE = True >> >> def function(arg): >> if VERBOSE: >>print("calling function with arg %r" % arg) >> process(arg) >> >> def caller(): >> VERBOSE = False >> function(1) >> >> - >> Pyt

Re: lambda in list comprehension acting funny

2012-07-13 Thread Chris Angelico
On Sat, Jul 14, 2012 at 2:46 AM, rusi wrote: > Ok let me restate: if python were to work that way (without the > global) we could say either > a Python chooses to have dynamic scoping of variables > or > b There is a bug in python's scoping rules Or c, there's a declaration at the top: from __fu

Re: lambda in list comprehension acting funny

2012-07-13 Thread rusi
On Jul 13, 9:12 pm, "Prasad, Ramit" wrote: > > VERBOSE = True > > > def function(arg): > >     if VERBOSE: > >        print("calling function with arg %r" % arg) > >     process(arg) > > > def caller(): > >     VERBOSE = False > >     function(1) > > > -

RE: lambda in list comprehension acting funny

2012-07-13 Thread Prasad, Ramit
> VERBOSE = True > > def function(arg): > if VERBOSE: >print("calling function with arg %r" % arg) > process(arg) > > def caller(): > VERBOSE = False > function(1) > > - > Python semantics: function sees VERBOSE False > Haskell

Re: lambda in list comprehension acting funny

2012-07-13 Thread rusi
To come back to the OPs question. Variables can be assigned. Or they can be bound. [C++ programmers will be familiar with the difference between initialization and assignment] List comprehensions are defined in terms of assignment to the local variable rather than binding. Hence the issue. Below

Re: lambda in list comprehension acting funny

2012-07-13 Thread rusi
On Jul 13, 11:36 am, Steven D'Aprano wrote: > On Thu, 12 Jul 2012 21:33:40 -0700, rusi wrote: > > On Jul 11, 11:41 am, Daniel Fetchinson > > wrote: > >> funcs = [ lambda x: x**i for i in range( 5 ) ] print funcs[0]( 2 ) > >> print funcs[1]( 2 ) > >> print funcs[2]( 2 ) > > >> This gives me > > >>

Re: lambda in list comprehension acting funny

2012-07-12 Thread Steven D'Aprano
On Thu, 12 Jul 2012 21:33:40 -0700, rusi wrote: > On Jul 11, 11:41 am, Daniel Fetchinson > wrote: >> funcs = [ lambda x: x**i for i in range( 5 ) ] print funcs[0]( 2 ) >> print funcs[1]( 2 ) >> print funcs[2]( 2 ) >> >> This gives me >> >> 16 >> 16 >> 16 >> >> When I was excepting >> >> 1 >> 2 >>

Re: lambda in list comprehension acting funny

2012-07-12 Thread rusi
On Jul 11, 11:41 am, Daniel Fetchinson wrote: > funcs = [ lambda x: x**i for i in range( 5 ) ] > print funcs[0]( 2 ) > print funcs[1]( 2 ) > print funcs[2]( 2 ) > > This gives me > > 16 > 16 > 16 > > When I was excepting > > 1 > 2 > 4 > > Does anyone know why? > > Cheers, > Daniel Your expectatio

Re: lambda in list comprehension acting funny

2012-07-12 Thread Rotwang
On 12/07/2012 04:59, Steven D'Aprano wrote: On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote: funcs = [ lambda x: x**i for i in range( 5 ) ] Here's another solution: from functools import partial funcs = [partial(lambda i, x: x**i, i) for i in range(5)] Notice that the arguments

Re: lambda in list comprehension acting funny

2012-07-12 Thread Ian Kelly
On Wed, Jul 11, 2012 at 9:59 PM, Steven D'Aprano wrote: > On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote: > >> funcs = [ lambda x: x**i for i in range( 5 ) ] > > Here's another solution: > > from functools import partial > funcs = [partial(lambda i, x: x**i, i) for i in range(5)] > >

Re: lambda in list comprehension acting funny

2012-07-12 Thread Steven D'Aprano
On Thu, 12 Jul 2012 09:44:15 +, Alister wrote: > On Wed, 11 Jul 2012 08:43:11 +0200, Daniel Fetchinson wrote: > >>> funcs = [ lambda x: x**i for i in range( 5 ) ] [...] > Having read Steve's explanation in the other thread (which I think has > finally flipped the light switch on lambda for m

Re: lambda in list comprehension acting funny

2012-07-12 Thread Alister
On Wed, 11 Jul 2012 08:43:11 +0200, Daniel Fetchinson wrote: >> funcs = [ lambda x: x**i for i in range( 5 ) ] >> print funcs[0]( 2 ) >> print funcs[1]( 2 ) >> print funcs[2]( 2 ) >> >> This gives me >> >> 16 16 16 >> >> When I was excepting >> >> 1 >> 2 >> 4 >> >> Does anyone know why? > > And m

Re: lambda in list comprehension acting funny

2012-07-12 Thread Robert Kern
On 7/11/12 9:21 PM, John Ladasky wrote: Exactly. It's threads like these which remind me why I never use lambda. I would rather give a function an explicit name and adhere to the familiar Python syntax, despite the two extra lines of code. I don't even like the name "lambda". It doesn't tel

Re: lambda in list comprehension acting funny

2012-07-12 Thread Mark Lawrence
On 12/07/2012 07:18, Steven D'Aprano wrote: On Wed, 11 Jul 2012 22:04:51 -0700, 8 Dihedral wrote: I have to make sure my functor to keep the state variable values for different objects that call the same functor to behave correctly in order to avoid passing extra parameters in various obj

Re: lambda in list comprehension acting funny

2012-07-11 Thread Franck Ditter
In article , Daniel Fetchinson wrote: > > funcs = [ lambda x: x**i for i in range( 5 ) ] > > print funcs[0]( 2 ) > > print funcs[1]( 2 ) > > print funcs[2]( 2 ) > > > > This gives me > > > > 16 > > 16 > > 16 > > > > When I was excepting > > > > 1 > > 2 > > 4 > > > > Does anyone know why? In Pyt

Re: lambda in list comprehension acting funny

2012-07-11 Thread Steven D'Aprano
On Wed, 11 Jul 2012 22:04:51 -0700, 8 Dihedral wrote: > I have to make sure my functor to keep the state variable values for > different objects that call the same functor to behave correctly in > order to avoid passing extra parameters in various objects using the > same functor. Yo dawg,

Re: lambda in list comprehension acting funny

2012-07-11 Thread John O'Hagan
On Wed, 11 Jul 2012 13:21:34 -0700 (PDT) John Ladasky wrote: > Exactly. It's threads like these which remind me why I never use lambda. I > would rather give a function an explicit name and adhere to the familiar > Python syntax, despite the two extra lines of code. I don't even like the > nam

Re: lambda in list comprehension acting funny

2012-07-11 Thread 88888 Dihedral
On Thursday, July 12, 2012 11:51:04 AM UTC+8, Steven D'Aprano wrote: > On Wed, 11 Jul 2012 20:39:45 -0700, 8 Dihedral wrote: > > > I'll contribute my way of python programming: > > > > def powerb(x, b): # > > return x**b > > Here's a shorter version: > > py> pow >

Re: lambda in list comprehension acting funny

2012-07-11 Thread Terry Reedy
On 7/11/2012 11:53 PM, Steven D'Aprano wrote: On Wed, 11 Jul 2012 21:05:30 -0400, Dennis Lee Bieber wrote: {non sequitur: I still recall my archaic C++ class with the OOAD assignment of designing said calculator -- we never had to implement one, just design the basic classes/methods/attributes

Re: lambda in list comprehension acting funny

2012-07-11 Thread Steven D'Aprano
On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote: > funcs = [ lambda x: x**i for i in range( 5 ) ] Here's another solution: from functools import partial funcs = [partial(lambda i, x: x**i, i) for i in range(5)] Notice that the arguments i and x are defined in the opposite order. T

Re: lambda in list comprehension acting funny

2012-07-11 Thread Steven D'Aprano
On Wed, 11 Jul 2012 20:39:45 -0700, 8 Dihedral wrote: > I'll contribute my way of python programming: > > def powerb(x, b): # > return x**b Here's a shorter version: py> pow > One functor is enough! Nothing we have been discussing in this thread has been a functor, either in the

Re: lambda in list comprehension acting funny

2012-07-11 Thread Steven D'Aprano
On Wed, 11 Jul 2012 21:05:30 -0400, Dennis Lee Bieber wrote: > {non sequitur: I still recall my archaic C++ class with the OOAD > assignment of designing said calculator -- we never had to implement > one, just design the basic classes/methods/attributes [on 3x5 cards] for > a four-banger. I manag

Re: lambda in list comprehension acting funny

2012-07-11 Thread 88888 Dihedral
On Thursday, July 12, 2012 12:34:33 AM UTC+8, Ian wrote: > On Wed, Jul 11, 2012 at 4:28 AM, Colin J. Williams wrote: > > I don't understand why you would expect 1, 2, 4. > > Because: > > funcs[0](2) == 2 ** 0 == 1 > funcs[1](2) == 2 ** 1 == 2 > funcs[2](2) == 2 ** 2 == 4 > > > Perh

Re: lambda in list comprehension acting funny

2012-07-11 Thread Daniel Fetchinson
>> You should not be using lambda in this case >> .for x in [2, 3]: >> .funcs = [x**ctr for ctr in range( 5 )] >> .for p in range(5): >> .print x, funcs[p] >> .print > > If you change the requirements, it's always easy to solve problems. But > it is the wrong problem that you ha

Re: lambda in list comprehension acting funny

2012-07-11 Thread Steven D'Aprano
On Wed, 11 Jul 2012 13:21:34 -0700, John Ladasky wrote: > Exactly. It's threads like these which remind me why I never use > lambda. I would rather give a function an explicit name and adhere to > the familiar Python syntax, despite the two extra lines of code. lambda is familiar Python syntax,

Re: lambda in list comprehension acting funny

2012-07-11 Thread Steven D'Aprano
On Wed, 11 Jul 2012 11:38:18 -0700, woooee wrote: > You should not be using lambda in this case > .for x in [2, 3]: > .funcs = [x**ctr for ctr in range( 5 )] > .for p in range(5): > .print x, funcs[p] > .print If you change the requirements, it's always easy to solve problem

Re: lambda in list comprehension acting funny

2012-07-11 Thread Hans Mulder
On 11/07/12 20:38:18, woooee wrote: > You should not be using lambda in this case > .for x in [2, 3]: > .funcs = [x**ctr for ctr in range( 5 )] > .for p in range(5): > .print x, funcs[p] > .print The list is called "funcs" because it is meant to contain functions. Your code doe

Re: lambda in list comprehension acting funny

2012-07-11 Thread John Ladasky
Exactly. It's threads like these which remind me why I never use lambda. I would rather give a function an explicit name and adhere to the familiar Python syntax, despite the two extra lines of code. I don't even like the name "lambda". It doesn't tell you what it is (unless you're John McCa

Re: lambda in list comprehension acting funny

2012-07-11 Thread Ian Kelly
On Wed, Jul 11, 2012 at 4:28 AM, Colin J. Williams wrote: > I don't understand why you would expect 1, 2, 4. Because: funcs[0](2) == 2 ** 0 == 1 funcs[1](2) == 2 ** 1 == 2 funcs[2](2) == 2 ** 2 == 4 > Perhaps parentheses will help the order of evaluation: > > funcs = [(lambda x: x**i) for i in

Re: lambda in list comprehension acting funny

2012-07-11 Thread Colin J. Williams
On 11/07/2012 2:41 AM, Daniel Fetchinson wrote: funcs = [ lambda x: x**i for i in range( 5 ) ] print funcs[0]( 2 ) print funcs[1]( 2 ) print funcs[2]( 2 ) This gives me 16 16 16 When I was excepting 1 2 4 Does anyone know why? Cheers, Daniel I don't understand why you would expect 1, 2,

Re: lambda in list comprehension acting funny

2012-07-11 Thread Daniel Fetchinson
>>> funcs = [ lambda x: x**i for i in range( 5 ) ] >>> print funcs[0]( 2 ) >>> >>> This gives me >>> 16 >>> >>> When I was excepting >>> 1 >>> >>> Does anyone know why? > >Just the way Python lambda expressions bind their variable > references. Inner 'i' references the outer scope's 'i' variabl

Re: lambda in list comprehension acting funny

2012-07-11 Thread Jurko Gospodnetić
Hi. funcs = [ lambda x: x**i for i in range( 5 ) ] print funcs[0]( 2 ) This gives me 16 When I was excepting 1 Does anyone know why? Just the way Python lambda expressions bind their variable references. Inner 'i' references the outer scope's 'i' variable and not its value 'at the tim

Re: lambda in list comprehension acting funny

2012-07-10 Thread Daniel Fetchinson
> funcs = [ lambda x: x**i for i in range( 5 ) ] > print funcs[0]( 2 ) > print funcs[1]( 2 ) > print funcs[2]( 2 ) > > This gives me > > 16 > 16 > 16 > > When I was excepting > > 1 > 2 > 4 > > Does anyone know why? And more importantly, what's the simplest way to achieve the latter? :) -- Psss,

lambda in list comprehension acting funny

2012-07-10 Thread Daniel Fetchinson
funcs = [ lambda x: x**i for i in range( 5 ) ] print funcs[0]( 2 ) print funcs[1]( 2 ) print funcs[2]( 2 ) This gives me 16 16 16 When I was excepting 1 2 4 Does anyone know why? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mail