473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why is this legal?


I tracked down a bug today that boiled down the "undecorate" part of
this "decorate-sort-undecorate":

cluster = [(c.resi,c) for c in cluster] # decorate
cluster.sort() # sort
cluster = [c for (c.resi,c) in cluster] # undecorate

That last line actually assigns c.resi for each c in cluster.

Here's a simple example of the same thing:

[mlerner@localhost mlerner]$ python
Python 2.3b2 (#1, Jul 3 2003, 14:20:37)
[GCC 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
class Foo: pass .... f1 = Foo(); f2 = Foo()
f1.x = 'go'; f2.x = 'od'
f1.x+f2.x 'good' things = [f for (f,f.x) in [(f1,'bo'),(f2,'gus')]]
f1.x+f2.x 'bogus'

Is there any reason you'd ever want to do this?

I use pychecker, but I've never actually looked at how it works.
Would it be hard to make pychecker detect something like this?

-michael
Jul 18 '05 #1
5 1309
Michael George Lerner wrote:
f1 = Foo(); f2 = Foo()
f1.x = 'go'; f2.x = 'od'
f1.x+f2.x

'good'

things = [f for (f,f.x) in [(f1,'bo'),(f2,'gus')]]
f1.x+f2.x

'bogus'

Is there any reason you'd ever want to do this?


I don't know if there's a *reason* why you'd want to do this.

But note that if you rewrote the list-comp as a for loop, you'd have the
same effect and probably not be surprised by it. It's simply a matter
that for loops (and list comprehensions) do not introduce a new scoping
level in Python.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #2
Michael George Lerner wrote:
cluster = [c for (c.resi,c) in cluster] # undecorate

That last line actually assigns c.resi for each c in cluster.

Is there any reason you'd ever want to do this?


Yes, I've done it many times.
Richard

Jul 18 '05 #3
On Wednesday 08 September 2004 02:05 pm, Michael George Lerner wrote:
I tracked down a bug today that boiled down the "undecorate" part of
this "decorate-sort-undecorate":

cluster = [(c.resi,c) for c in cluster] # decorate
cluster.sort() # sort
cluster = [c for (c.resi,c) in cluster] # undecorate

That last line actually assigns c.resi for each c in cluster.
So if you don't want to assign to c.resi don't use it as a loop
variable:
cluster = [c for (ignored,c) in cluster] # undecorate
or
cluster = [item[1] for item in cluster] # undecorate
Is there any reason you'd ever want to do this?


There is a big reason to want it to stay this way. The rule that
"each pass through the loop rebinds the loop variable (or tuple of
loop variables)" is simple, and clear. For that reason alone, we
don't want to ever change it.

Gary Herron
Jul 18 '05 #4
Gary Herron <gh*****@islandtraining.com> wrote:
On Wednesday 08 September 2004 02:05 pm, Michael George Lerner wrote:
I tracked down a bug today that boiled down the "undecorate" part of
this "decorate-sort-undecorate":

cluster = [(c.resi,c) for c in cluster] # decorate
cluster.sort() # sort
cluster = [c for (c.resi,c) in cluster] # undecorate

That last line actually assigns c.resi for each c in cluster.
So if you don't want to assign to c.resi don't use it as a loop
variable:
cluster = [c for (ignored,c) in cluster] # undecorate
or
cluster = [item[1] for item in cluster] # undecorate
Yup. I rewrote it as
cluster = [c for (resi,c) in cluster] # undecorate
once I found the bug.

I think I was just annoyed that it was so easy to write the
undecorate line by copying the decorate line and moving terms
around .. easy, but broken.

You and Jeff Shannon have convinced me that I just need to make
sure that I don't forget that a list comprehension is really
just a cute for loop.

It still seems a little jarring, though.

Thanks,

-michael
Is there any reason you'd ever want to do this?

There is a big reason to want it to stay this way. The rule that
"each pass through the loop rebinds the loop variable (or tuple of
loop variables)" is simple, and clear. For that reason alone, we
don't want to ever change it. Gary Herron

Jul 18 '05 #5
Michael George Lerner <ml*****@NOumichSPAM.edu> wrote:
...
Heck, maybe I'll even download Python 2.4 so I can say
cluster.sort(key=lambda c: c.resi)


Or better,
cluster.sort(key=operator.attrgetter('resi'))

This way it will really fly!
Alex
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Young | last post by:
I'm seeing a problem with IE running on OS-X. When the user logs in to the website (ASP/IIS5), a Session is established properly, but somewhere along the line, a new Session is created and...
2
by: James | last post by:
Are there any style elements that work on select elements on Apple/IE? I want colored text, colored background, or just about anything colored in a select drop-down but can't seem to make it work...
4
by: Peter Larsson | last post by:
Hello everybody, I'm developing a system for a company that has production at two different locations. The production is stored in an Access database at each location, and I need to find a way...
1
by: Wiktor Zychla | last post by:
is it legal to distribute axshdocvw, shdocvw and mshtml.dll with my application? is it legal to distribute Microsoft.mshtml.dll from 'Primary Interop Assemblies' folder (I assume it comes with...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
3
by: Roy Osherove | last post by:
Hi folks. I have an ASP.Net application that runs a .Net dll that uses WMI and ADSI(both managed) to connect to a given IIS root and search through it. When not using the ASP.Net client, but...
2
by: Greg Donald | last post by:
Is it legal syntax to use <> instead of != in a Postgres query? I didn't see it listed on: http://www.postgresql.org/docs/7.4/static/sql-syntax.html but wanted to ask to make sure.
3
by: millind | last post by:
I have a small issue. I have a html page which i need to print it legal size. My default printer setting is letter. How can i set a script which will print just this page in legal without change...
53
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
2
by: Army1987 | last post by:
Is this program legal C89? /* no headers included */ int main(void) { if (sizeof (exit(0), 0), ((void (* )(int))&exit)( (puts((const char *)"hello, world"), 0) ), 0) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.