Language Design: list_for scope?

Paddy paddy3118 at netscape.net
Fri Jul 21 13:07:10 EDT 2006


guthrie wrote:
> I'm pretty new to Python, and trying to parse the grammar.
>
> Q: What is the scope of the testlist in a list_for?
>
> For example;
> Instead of;
>       for x in [ x in dict if dict[x]=="thing" ]:
> in this:
>       for x in dict and dict[x]=="thing":
> x is undefined.
>
> And why doesn't this work:
> 	for x in dict if dict[x]=="thing":
>
> Any insights/hints on why it is broken?
>
> Thanks,
> Gregory
> ----------------------------------------
> http://docs.python.org/ref/grammar.txt:
> list_for ::=
>               "for" expression_list "in" testlist
>                [list_iter]
> testlist ::=
>               test ( "," test )* [ "," ]
> list_iter ::=
>               list_for | list_if
> list_if ::=
>               "if" test [list_iter]
>
>
> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

It is hard to answer your "Why doesn't this wirk question|", and it
must be hard to approach Python as a new user by looking at the
grammer.
Why not try looking at examples, trying them out in the Idle IDE, and
only then compare  your working examples to the grammer?

Here is a page with examples you could type in or vary in the
interpreter:

http://nltk.sourceforge.net/tutorial/advpython/nochunks.html#list_comprehensions

Cheers, Paddy.




More information about the Python-list mailing list