Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Another python pearl
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nils Bruin  
View profile  
 More options Jan 17 2011, 2:39 pm
From: Nils Bruin <bruin.n...@gmail.com>
Date: Mon, 17 Jan 2011 11:39:02 -0800 (PST)
Local: Mon, Jan 17 2011 2:39 pm
Subject: Another python pearl
Another case where Python 2.*'s leaking of iteration parameters leads
to really counterintuitive results:

This more or less does the expected thing:

>>> [ (a,b) for a in range(10) for b in range(10) if a==b ]

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8,
8), (9, 9)]

Although there's a small catch. Even though the for ... if ...
construct binds variables that are used to the left of it, and hence
in the above you might expect "a" to be the "inner variable", it is
not:

>>> [ (a,b) for a in range(3) for b in range(3)]

[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2,
2)]

as it turns out, "if"s can be freely intermingled with "for" clauses,
so we can also execute

>>> b
2
>>> [ (a,b) for a in range(10) if a==b for b in range(10) ]

[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2,
8), (2, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6),
(9, 7), (9, 8), (9, 9)]
>>> b

9

This problem happily goes away in Python 3, where the semantics are
the same as (and this already works in Python 2.*):

>>> list( (a,b) for a in range(10) for b in range(10) if a==b )
>>> list( (a,b) for a in range(3) for b in range(3) )
>>> list( (a,b) for a in range(10) if a==b for b in range(10) )

where the last happily produces an error, even if b is globally bound
already. That is actually slightly better than the still slightly
confusing

>>> b=2
>>> list(list ( (a,b) for b in range(10)) for a in range(10) if a==b)

[[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2,
8), (2, 9)]]

(which is recognizable as bad programming style)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »