list IndexError

Jeff Shannon jeff at ccvcorp.com
Thu Dec 23 13:15:05 EST 2004


Grant Edwards wrote:

>On 2004-12-23, Steven Bethard <steven.bethard at gmail.com> 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







More information about the Python-list mailing list