Re: generator expression works in shell, NameError in script

2009-06-29 Thread alito
On Jun 18, 11:56 pm, nn prueba...@latinmail.com wrote: On Jun 18, 8:38 am, guthrie grguth...@gmail.com wrote: On Jun 17, 6:38 pm, Steven Samuel Cole steven.samuel.c...@gmail.com wrote: Still don't really understand why my initial code didn't work, though... Your code certainly looks

Re: generator expression works in shell, NameError in script

2009-06-25 Thread Aahz
In article 13e776b0-6ca8-479e-ba82-eb5b7eb8f...@a5g2000pre.googlegroups.com, ssc steven.samuel.c...@gmail.com wrote: On Jun 18, 10:49=A0am, Jon Clements jon...@googlemail.com wrote: Why are you doing this? I'm assuming a code to title look up is required (don't forget military, royal and

Re: generator expression works in shell, NameError in script

2009-06-23 Thread ssc
On Jun 18, 10:49 am, Jon Clements jon...@googlemail.com wrote: Why are you doing this? I'm assuming a code to title look up is required (don't forget military, royal and honorable titles etc... :) ) I'm in New Zealand. Hardly any need for military titles, rarely any for royal and damn sure

Re: generator expression works in shell, NameError in script

2009-06-21 Thread guthrie
On Jun 18, 11:28 pm, greg g...@cosc.canterbury.ac.nz wrote: nn wrote: This is certainly an odd one. This code works fine under 2.6 but fails in Python 3.1. class x: ...     lst=[2] ...     gen=[lst.index(e) for e in lst] In 3.x it was decided that the loop variables in a list

Re: generator expression works in shell, NameError in script

2009-06-21 Thread Miles Kaufmann
On Jun 19, 2009, at 8:45 AM, Bruno Desthuilliers wrote: class Foo(object): ... bar = ['a', 'b', 'c'] ... baaz = list((b, b) for b in bar) but it indeed looks like using bar.index *in a generator expression* fails (at least in 2.5.2) : class Foo(object): ... bar = ['a', 'b',

Re: generator expression works in shell, NameError in script

2009-06-21 Thread greg
guthrie wrote: -- I don't get this - the only local loop variable is e, not lst. Yes, but the whole list comprehension gets put into a nested function, including the part that evaluates lst. (It's not strictly *necessary* to do that, but that's the way it happens to be implemented at the

Re: generator expression works in shell, NameError in script

2009-06-19 Thread Bruno Desthuilliers
Emile van Sebille a écrit : On 6/17/2009 3:54 PM ssc said... Wow! Didn't expect that kind of instant support. Thank you very much, I'll give both zip and enumerate a try. The code I've shown is actually copied pretty straight from a Django form class, but I didn't want to mention that as not

Re: generator expression works in shell, NameError in script

2009-06-19 Thread Mark Dickinson
On Jun 19, 1:45 pm, Bruno Desthuilliers bruno. 42.desthuilli...@websiteburo.invalid wrote: [...] but it indeed looks like using bar.index *in a generator expression* fails (at least in 2.5.2) :   class Foo(object): ...     bar = ['a', 'b', 'c'] ...     baaz = list((bar.index(b), b) for b

Re: generator expression works in shell, NameError in script

2009-06-19 Thread ryles
Does the generator expression have its own little namespace or so ? Yes, generators create a new scope: http://docs.python.org/reference/expressions.html#grammar-token-generator_expression -- http://mail.python.org/mailman/listinfo/python-list

Re: generator expression works in shell, NameError in script

2009-06-19 Thread greg
ryles wrote: However, in 3.0 list comprehensions are actually treated as list(generator). That's not quite true -- it would be rather inefficient if it was. The code generated for the LC is much the same as it was in 2.x. But the whole LC is put into a nested function so that the loop

Re: generator expression works in shell, NameError in script

2009-06-18 Thread guthrie
On Jun 17, 6:38 pm, Steven Samuel Cole steven.samuel.c...@gmail.com wrote: Still don't really understand why my initial code didn't work, though... Your code certainly looks reasonable, and looks to me like it should work. The comment of partial namespace is interesting, but unconvincing (to me)

Re: generator expression works in shell, NameError in script

2009-06-18 Thread nn
On Jun 18, 8:38 am, guthrie grguth...@gmail.com wrote: On Jun 17, 6:38 pm, Steven Samuel Cole steven.samuel.c...@gmail.com wrote: Still don't really understand why my initial code didn't work, though... Your code certainly looks reasonable, and looks to me like it should work. The comment

Re: generator expression works in shell, NameError in script

2009-06-18 Thread ryles
On Jun 18, 9:56 am, nn prueba...@latinmail.com wrote: On Jun 18, 8:38 am, guthrie grguth...@gmail.com wrote: On Jun 17, 6:38 pm, Steven Samuel Cole steven.samuel.c...@gmail.com wrote: Still don't really understand why my initial code didn't work, though... Your code certainly looks

Re: generator expression works in shell, NameError in script

2009-06-18 Thread greg
ssc wrote: class SignupForm(Form): titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms',] title_choices = [(0, '')] + list((titles.index(t)+1, t) for t in titles) Does the generator expression have its own little namespace or so ? Yes. The generator expression is a function, with its own

Re: generator expression works in shell, NameError in script

2009-06-18 Thread greg
nn wrote: This is certainly an odd one. This code works fine under 2.6 but fails in Python 3.1. class x: ... lst=[2] ... gen=[lst.index(e) for e in lst] In 3.x it was decided that the loop variables in a list comprehension should be local, and not leak into the surrounding scope.

generator expression works in shell, NameError in script

2009-06-17 Thread ssc
Hello, I am trying to generate this list of tuples: [(0, ''), (1, 'Dr'), (2, 'Miss'), (3, 'Mr'), (4, 'Mrs'), (5, 'Ms')] My code works fine in the Python shell: titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms',] title_choices = [(0, '')] + list((titles.index(t)+1, t) for t in titles) title_choices

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Emile van Sebille
On 6/17/2009 3:19 PM ssc said... Hello, I am trying to generate this list of tuples: [(0, ''), (1, 'Dr'), (2, 'Miss'), (3, 'Mr'), (4, 'Mrs'), (5, 'Ms')] My code works fine in the Python shell: titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms',] title_choices = [(0, '')] + list((titles.index(t)+1, t)

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Chris Rebert
On Wed, Jun 17, 2009 at 3:19 PM, sscsteven.samuel.c...@gmail.com wrote: Hello, I am trying to generate this list of tuples: [(0, ''), (1, 'Dr'), (2, 'Miss'), (3, 'Mr'), (4, 'Mrs'), (5, 'Ms')] My code works fine in the Python shell: titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms',] title_choices

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Jason Tackaberry
On Wed, 2009-06-17 at 15:38 -0700, Chris Rebert wrote: See what Emile said, but here's a nicer way to code it, IMHO: titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms'] title_choices = zip(range(len(titles)+1), ['']+titles) zip() to the rescue! How about: enumerate([''] + titles) --

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Emile van Sebille
On 6/17/2009 3:41 PM Jason Tackaberry said... How about: enumerate([''] + titles) or perhaps, depending on usage... list(enumerate(titles)) Emile -- http://mail.python.org/mailman/listinfo/python-list

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Jon Clements
On Jun 17, 11:19 pm, ssc steven.samuel.c...@gmail.com wrote: Hello, I am trying to generate this list of tuples: [(0, ''), (1, 'Dr'), (2, 'Miss'), (3, 'Mr'), (4, 'Mrs'), (5, 'Ms')] My code works fine in the Python shell: titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms',] title_choices = [(0,

Re: generator expression works in shell, NameError in script

2009-06-17 Thread ssc
Wow! Didn't expect that kind of instant support. Thank you very much, I'll give both zip and enumerate a try. The code I've shown is actually copied pretty straight from a Django form class, but I didn't want to mention that as not to dilute the conversation. Don't think it matters, anyway. This

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Chris Rebert
On Wed, Jun 17, 2009 at 3:54 PM, sscsteven.samuel.c...@gmail.com wrote: Wow! Didn't expect that kind of instant support. Thank you very much, I'll give both zip and enumerate a try. The code I've shown is actually copied pretty straight from a Django form class, but I didn't want to mention

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Emile van Sebille
On 6/17/2009 3:54 PM ssc said... Wow! Didn't expect that kind of instant support. Thank you very much, I'll give both zip and enumerate a try. The code I've shown is actually copied pretty straight from a Django form class, but I didn't want to mention that as not to dilute the conversation.

Re: generator expression works in shell, NameError in script

2009-06-17 Thread Steven Samuel Cole
Both zip and enumerate do the trick. Thanks for the pointers, that's 2 more built-ins under my belt :-) Still don't really understand why my initial code didn't work, though... Thanks everyone! :-) -- http://mail.python.org/mailman/listinfo/python-list