By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,497 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Language Design: list_for scope?

P: n/a
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 =----
Jul 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
First things first:

Don't name a variable "dict" because if you do it shadows
the built in dict function (same goes for list, str, ...).
This WILL bite you later, so start now by not naming
variables by any built in names.

Now to your question:

for x in <something>: requires <somethingbe an iterable
(e.g. list, tuple, iterable class instance, etc) so there
is something that it can loop over one object at at time.
>for x in [ x in dict if dict[x]=="thing" ]:
needs to be written (if I'm understanding what you are
doing) as:

for x in [x for x in d if d[x]=="thing"]:

[ x for x in dict if dict[x]=="thing" ] is a list
comprehension that will resolve to a list of keys from the
dictionary where the values are "thing". This way
the result of the list comprehension is a list which is
something the outside for loop can iterate over.

Example:
>>d={1:'x',2:'thing',3:'thing',4:'thing',5:'thing' , \
6:'z',7:'thing',8:'thing',9:'y'}
>>[x for x in d if d[x]=="thing"]
[2, 3, 4, 5, 7, 8]

-Larry Bates
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 =----
Jul 21 '06 #2

P: n/a

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...comprehensions

Cheers, Paddy.

Jul 21 '06 #3

P: n/a


Larry Bates wrote:
First things first:

Don't name a variable "dict" because if you do it shadows
the built in dict function (same goes for list, str, ...).
This WILL bite you later, so start now by not naming
variables by any built in names.
-- Thanks, got it!
>
Now to your question:

for x in <something>: requires <somethingbe an iterable
(e.g. list, tuple, iterable class instance, etc) so there
is something that it can loop over one object at at time.

>>for x in [ x in dict if dict[x]=="thing" ]:


needs to be written (if I'm understanding what you are
doing) as:

for x in [x for x in d if d[x]=="thing"]:
-- Ywes, I understand the syntax, but think it is repetitive and cumbersome.

Just note the "for x in x for x in ..."
no new semantics of the desired iteration set given, all redundant
syntax.

I still wonder why one cannot qualify a for list iteration object with
an list_if

Greg

>>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 =----

----== 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 =----
Jul 21 '06 #4

P: n/a
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.
In the above two examples, x is a regular variable (global or local
depending on whether it's in a function). It will remain undefined
until the first time something gets assigned to it.

In your first example (where you appear to have left out a for), it get
bound inside the list comprehension, after evaluating 'dict' but before
evaluating the test 'dict[x]=="thing"'.

I doubt the second example does what you want it to, but here's what
happens. Python tries to evaluate the expression 'dict and
dict[x]=="thing"' first, before assigning anything to x. Because that
expression uses x, you get an undefined error. If it hadn't had the
undefined error, it would have tried to iterate over the result (which
would have been True or False), which would have raised a TypeError
exception.

And why doesn't this work:
for x in dict if dict[x]=="thing":
Because it would would just be another way to spell:

for x in dict:
if dict[x] == "thing:

Yes, we've heard all the arguments before. The Python language
designers like to have only one way to do things when they can, and in
this case they felt that none of the objections (an extra level of
indentation or unsightly continue) warranted a new spelling.

I recall there was to be a (prerejected) PEP submitted about this
recently, but it doesn't appear to be up.

Any insights/hints on why it is broken?
It's not broken :)
Carl Banks

Jul 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.