iainking at gmail.com
Tue Nov 25 12:42:47 CET 2008
On Nov 25, 11:29 am, Iain King <iaink... at gmail.com> wrote:
> On Nov 17, 7:41 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> > > It doesn't matter as none of this is valid Python. In Python you have to
> > > write
> > > array[x1] = False
> > > array[x2] = False
> > Uh...not so much...
> > >>> a = [1,2,3,4,5]
> > >>> x1, x2 = 1, 3
> > >>> a[x1] = a[x2] = False
> > >>> a
> > [1, False, 3, False, 5]
> > Works for me.
> > To the OP, I think rather than cluttering my code, I'd just
> > create a loop
> > for i in [x1,x2,x3,x4,...x1024]:
> > a[i] = False
> > From Diez's disassembly of it (as an aside, nifty little intro
> > to dis.dis()...thanks, Diez!), it looks like it boils down to "is
> > DUP_TOP faster than LOAD_CONST" because the rest of the
> > operations. At this point, it's pretty nitty-gritty.
> > Unless the code is in an inner loop somewhere, the simple loop
> > should be more than fast enough. Without knowing the source of
> > the [x1,...] index variables, it's hard to tell if there's a more
> > optimal way to do this.
> > -tkc
> The loop is much nicer, especially as your array gets longer. The
> for i in xrange(array):
> array[i] = False
> will set the entire array.
> Regarding your original question:
> array[x1] = array[x2] = False
> array[x1] = False
> array[x2] = False
> These two blocks are functionally the same when you are setting to
> True or False (or any immutable), but are not if setting to
> immutables, which could give you some real head-scratching bugs if you
> were unaware of the difference - the first version assigns the same
> object to both names:
> >>> array[x1] = array[x2] = 
> >>> array[x1].append("Hi")
> >>> array[x2]
...and of course, the second time I say 'immutable' I mean 'mutable'.
Hopefully the example was clearer than the text.
More information about the Python-list