ckd: (cpu)
[personal profile] ckd
#!/usr/bin/env python

# set up items, define is_type_x functions, etc

list_a = list_b = list_c = []

for item in items:
    if is_type_a(item):
        list_a.append(item)
    elif is_type_b(item):
        list_b.append(item)
    elif is_type_c(item):
        list_c.append(item)
    else:
        print 'wtf is this?',item
The a = b = c = [] construct makes all three variables references to the same empty list. list.append() modifies the list in place. After this loop runs, all three lists will contain all of the items except those that aren't type a, b, or c.

The most concise fix is to use list_a, list_b, list_c = [], [], [] as a replacement.

Don't ask how long I was beating my head against this one.

Date: 2006-10-02 20:16 (UTC)
From: [identity profile] melted-snowball.livejournal.com
I think I have made that error, in some sense, before.

Date: 2006-10-02 20:20 (UTC)
cos: (Default)
From: [personal profile] cos
I don't know Python, so I thought it would be some syntax-related thing I wouldn't spot, but I looked anyway... and that was my first (and only) guess, before clicking on the cut.

Date: 2006-10-02 21:38 (UTC)
From: [identity profile] schulman.livejournal.com
Been there. I think of these as "this is not a pipe" errors -- the pointer to the thing is not the thing itself.

(Beating my head against the keyboard brings out my inner philosopher.)

Date: 2006-10-02 23:02 (UTC)
secretagentmoof: (Default)
From: [personal profile] secretagentmoof
That was easy to spot; I mean, the shebang pretty obviously mentions python.

Date: 2006-10-04 00:21 (UTC)
From: [identity profile] dd-b.livejournal.com
I'm glad somebody covered this before I got to it :-).

Of course, one might claim that that is not a *subtle* error.

My particular forehead-basher this afternoon involves tcl and expect. I think I'm developing a good-sized lump.

Date: 2006-10-03 05:01 (UTC)
From: [identity profile] tober.livejournal.com
In my book, that it's so easy to commit this error is a language design flaw. Languages shouldn't have syntaxes that lull you into believing that a reference to a foo actually is a foo. Alternately, if they're going to insist on this syntax (my recollection is that in python pretty much the only type of variable is a reference to an object - but I am totally not a python guy) then all object literals (such as []) should be inherently completely idempotent and immutable - maybe with automatic copy on write.

Date: 2006-10-04 00:26 (UTC)
secretagentmoof: (Default)
From: [personal profile] secretagentmoof
precisely. In perl, if you really want $a, $b, and $c to point to the same array object, you have to go out of your way to do so:

$z = $y = $x = \( ); (or, alternately, $z = $y = $x = []; - but that's specifically defined as returning a reference to an array.)

IMNSHO, if you want to have a reference/pointer/whateveryouwannacallit, you should explicitly ask for one.

Date: 2006-10-03 08:24 (UTC)
From: [identity profile] webzombi.livejournal.com
i have no idea what any of that means...but as long as it's workin'...yay!

(thank goodness for folks like you)

:-)

Date: 2006-10-03 12:00 (UTC)
From: [personal profile] ron_newman
Don't blame Python. It would be equally easy to make this mistake in Ruby.

Profile

ckd: small blue foam shark (Default)
blue shark of friendliness

January 2026

S M T W T F S
    123
4 5678910
11121314151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated 2026-01-23 15:24
Powered by Dreamwidth Studios