Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-19 Thread skip
On 5/18/2011 10:19 AM, Nadeem Vawda wrote: > I'm not sure why you would encounter code like that in the first place. > Surely any code of the form: > > ''.join(c for c in my_string) > > would just return my_string? Or am I missing something? You might more-or-less legitimately encounter it if t

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-19 Thread Guido van Rossum
On Wed, May 18, 2011 at 2:34 PM, Victor Stinner wrote: > Le mercredi 18 mai 2011 à 16:19 +0200, Nadeem Vawda a écrit : >> I'm not sure why you would encounter code like that in the first place. > > Well, I found the STORE_FAST/LOAD_FAST "issue" while trying to optimize > the this module which reim

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-19 Thread Greg Ewing
Victor Stinner wrote: I suppose that you have the current value of range(1) on the stack: DUP_TOP; BINARY_MULTIPLY; gives you the square. You don't need the x variable (LOAD_FAST/STORE_FAST). That seems far too special-purpose to be worth it to me. -- Greg

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-19 Thread Victor Stinner
Le mercredi 18 mai 2011 à 21:44 -0400, Terry Reedy a écrit : > On 5/18/2011 5:34 PM, Victor Stinner wrote: > > You initial example gave me the impression that the issue has something > to do with join in particular, or even comprehensions in particular. It > is really about for loops. > > >>>

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-19 Thread Victor Stinner
Le jeudi 19 mai 2011 à 10:47 +1200, Greg Ewing a écrit : > Victor Stinner wrote: > > >squares = (x*x for x in range(1)) > > What bytecode would you optimise that into? I suppose that you have the current value of range(1) on the stack: DUP_TOP; BINARY_MULTIPLY; gives you the square.

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-19 Thread Nick Coghlan
On Thu, May 19, 2011 at 7:34 AM, Victor Stinner wrote: > But it is slower whereas I read somewhere than generators are faster > than loops. Are you sure it wasn't that generator expressions can be faster than list comprehensions (if the memory savings are significant)? Or that a reduction functi

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Terry Reedy
On 5/18/2011 5:37 PM, Amaury Forgeot d'Arc wrote: Hi, 2011/5/18 Terry Reedy: On 5/18/2011 10:19 AM, Nadeem Vawda wrote: I'm not sure why you would encounter code like that in the first place. Surely any code of the form: ''.join(c for c in my_string) would just return my_string? Or am

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Terry Reedy
On 5/18/2011 5:34 PM, Victor Stinner wrote: You initial example gave me the impression that the issue has something to do with join in particular, or even comprehensions in particular. It is really about for loops. squares = (x*x for x in range(1)) >>> dis('for x in range(3): y = x

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Greg Ewing
Victor Stinner wrote: squares = (x*x for x in range(1)) What bytecode would you optimise that into? -- Greg ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Amaury Forgeot d'Arc
Hi, 2011/5/18 Terry Reedy : > On 5/18/2011 10:19 AM, Nadeem Vawda wrote: > >> I'm not sure why you would encounter code like that in the first place. >> Surely any code of the form: >> >>     ''.join(c for c in my_string) >> >> would just return my_string? Or am I missing something? > > Good quest

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Victor Stinner
Le mercredi 18 mai 2011 à 16:19 +0200, Nadeem Vawda a écrit : > I'm not sure why you would encounter code like that in the first place. Well, I found the STORE_FAST/LOAD_FAST "issue" while trying to optimize the this module which reimplements rot13 using a dict in Python 3: d = {} for c in (65, 9

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Terry Reedy
On 5/18/2011 10:19 AM, Nadeem Vawda wrote: I'm not sure why you would encounter code like that in the first place. Surely any code of the form: ''.join(c for c in my_string) would just return my_string? Or am I missing something? Good question. Anything useful like "'-'.join(c for c in

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Nadeem Vawda
On Wed, May 18, 2011 at 2:21 PM, Victor Stinner wrote: > ''.join(c for c in 'abc') and ''.join([c for c in 'abc']) do create a > temporary c variable. I'm not sure why you would encounter code like that in the first place. Surely any code of the form: ''.join(c for c in my_string) would jus

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Nick Coghlan
On Wed, May 18, 2011 at 10:21 PM, Victor Stinner wrote: > What do you think? Is it useless and/or stupid? I wouldn't call it useless or stupid - merely "lost in the noise". In small cases, I expect it would be swamped completely by the high fixed overhead of entering the new scope and in all gene

Re: [Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Benjamin Peterson
2011/5/18 Victor Stinner : > Hi, > > ''.join(c for c in 'abc') and ''.join([c for c in 'abc']) do create a > temporary c variable. In this case, the variable is useless and requires > two opcodes: STORE_FAST(c), LOAD_FAST(c). The variable is not available > outside the list comprehension/generator.

[Python-Dev] Don't set local variable in a list comprehension or generator

2011-05-18 Thread Victor Stinner
Hi, ''.join(c for c in 'abc') and ''.join([c for c in 'abc']) do create a temporary c variable. In this case, the variable is useless and requires two opcodes: STORE_FAST(c), LOAD_FAST(c). The variable is not available outside the list comprehension/generator. I would like to remove the variable