Why is this legal?

Michael George Lerner mlerner at NOumichSPAM.edu
Wed Sep 8 23:05:02 CEST 2004


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



More information about the Python-list mailing list