Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: list IndexError

Reply
Thread Tools

Re: list IndexError

 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      12-23-2004
Fredrik Lundh wrote:

>Mike C. Fletcher wrote:
>
>
>
>>>yeah actually i saw what Fedrik had to say above. I created a sliced
>>>copy of the l & did my homework within the for loop
>>>
>>>
>>>

>>You might want to check it again before you hand it in ...
>>
>>

....

>that's not the code he quoted in the mail you replied to, though...
>
>

Ah, my mistake, I missed the [:] after the source argument that was
taking a copy... which brings up the question, how many other people
would miss it? Guess it's just a matter of how often you see the
pattern, so you begin to expect the [:] and either look for it or take
it for granted (hmm, another source of bugs?).

Apologies for any unnecessary alarm,
Mike

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      12-23-2004
Mike C. Fletcher wrote:
> Ah, my mistake, I missed the [:] after the source argument that was
> taking a copy... which brings up the question, how many other people
> would miss it?


Too many. This is why I greatly prefer

list(lst)

to

lst[:]

It's also clearer to me. Do I really want a "slice" of the list? No, I
want a list copy of the list...

Steve
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      12-23-2004
On 2004-12-23, Steven Bethard <(E-Mail Removed)> wrote:

>> Ah, my mistake, I missed the [:] after the source argument
>> that was taking a copy... which brings up the question, how
>> many other people would miss it?

>
> Too many. This is why I greatly prefer
>
> list(lst)


To me, that's just as non-obvious. I would have guessed that
calling list() on a list was a noop. I would be wrong.
Surprised, but wrong.

> to
>
> lst[:]
>
> It's also clearer to me. Do I really want a "slice" of the
> list? No, I want a list copy of the list...


Wouldn't the clearest way to get a copy would be to do
something like:

copy(lst) # I still think copy.copy() is a bit verbose...
or
lst.copy() # doesn't exist, but I think it should

--
Grant Edwards grante Yow! I had pancake makeup
at for brunch!
visi.com
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-23-2004
Grant Edwards wrote:
> Wouldn't the clearest way to get a copy would be to do
> something like:
>
> copy(lst) # I still think copy.copy() is a bit verbose...


True, true. Maybe you could lobby for copy as a builtin in Python 3000?

Steve
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-23-2004
Grant Edwards wrote:
> I would have guessed that calling list() on a list
> was a noop. I would be wrong. Surprised, but wrong.


I guess it's probably worth pointing out that most builtin mutable types
can be copied using the type constructor:

py> def check(obj):
.... copy = type(obj)(obj)
.... print id(obj), id(copy)
.... return copy
....
py> check([1, 2])
18124312 18124752
[1, 2]
py> check({1:2})
18102720 18126720
{1: 2}
py> check(set([1, 2]))
9675672 9675504
set([1, 2])

For immutable types, this is indeed basically a noop:

py> check(12)
3303412 3303412
12
py> check('12')
18120128 18120128
'12'
py> check((1, 2))
18122552 18122552
(1, 2)

Steve
 
Reply With Quote
 
Jeff Shannon
Guest
Posts: n/a
 
      12-23-2004
Grant Edwards wrote:

>On 2004-12-23, Steven Bethard <(E-Mail Removed)> wrote:
>
>
>
>>>Ah, my mistake, I missed the [:] after the source argument
>>>that was taking a copy... which brings up the question, how
>>>many other people would miss it?
>>>
>>>

>>Too many. This is why I greatly prefer
>>
>> list(lst)
>>
>>

>
>To me, that's just as non-obvious. I would have guessed that
>calling list() on a list was a noop. I would be wrong.
>Surprised, but wrong.
>
>


It makes a lot more sense when you remind yourself that list() et al are
not conversion functions, but rather class constructors. (This is
complicated by the fact that in old Pythons, int() and float() *were*
conversion functions... but are not so any more.)

Given a user-defined class, I think it wouldn't be any surprise to see that

class Spam:
# [....]

s1 = Spam( ... )
s2 = Spam(s1)

results in two (probably almost identical) instances of Spam. Using
list() to create a copy of a list is analogous, but we're used to
thinking of list() as a converter rather than a constructor...

Jeff Shannon
Technician/Programmer
Credit International




 
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
Can't get around "IndexError: list index out of range" erikwickstrom@gmail.com Python 36 10-11-2006 04:56 AM
returning a list: IndexError shama.bell@gmail.com Python 5 03-31-2005 05:20 PM
Re: list IndexError Ishwor Python 2 12-27-2004 11:23 AM
list IndexError Ishwor Python 6 12-23-2004 03:57 PM
Re: list IndexError Ishwor Python 4 12-23-2004 03:16 PM



Advertisments