generator expression works in shell, NameError in script

Chris Rebert clp2 at rebertia.com
Thu Jun 18 01:11:19 CEST 2009


On Wed, Jun 17, 2009 at 3:54 PM, ssc<steven.samuel.cole at 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 that as not to dilute the
> conversation. Don't think it matters, anyway. This is the relevant
> excerpt:
>
> from django.forms import Form
>
> class SignupForm(Form):
>
>    titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms',]
>    title_choices   = [(0, '')] + list((titles.index(t)+1, t) for t in
> titles)
>
> Now that I look at it again, it seems odd to me to not have the code
> e.g. in __init__(...), but just 'class-global'.
> Still, that does not seem a reason for titles not to be not defined,
> as I do define it just in the line above.
>
> Does the generator expression have its own little namespace or so ?

No, which leads to the common "WTF?" reaction upon seeing stuff like:

>>> funcs = ((lambda: x) for x in range(7))
>>> list_of = list(funcs)
>>> list_of[0]()
6

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list