Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why is this legal?

Reply
Thread Tools

Why is this legal?

 
 
Michael George Lerner
Guest
Posts: n/a
 
      09-08-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@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
 
Reply With Quote
 
 
 
 
Jeff Shannon
Guest
Posts: n/a
 
      09-08-2004
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

 
Reply With Quote
 
 
 
 
richard
Guest
Posts: n/a
 
      09-08-2004
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

 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      09-09-2004
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


 
Reply With Quote
 
Michael George Lerner
Guest
Posts: n/a
 
      09-09-2004
Gary Herron <> 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



 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      09-09-2004
Michael George Lerner <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57