Re: list IndexError

Discussion in 'Python' started by Mike C. Fletcher, Dec 23, 2004.

  1. 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
     
    Mike C. Fletcher, Dec 23, 2004
    #1
    1. Advertising

  2. 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
     
    Steven Bethard, Dec 23, 2004
    #2
    1. Advertising

  3. On 2004-12-23, Steven Bethard <> 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
     
    Grant Edwards, Dec 23, 2004
    #3
  4. 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
     
    Steven Bethard, Dec 23, 2004
    #4
  5. copying builtin types (WAS: list IndexError)

    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
     
    Steven Bethard, Dec 23, 2004
    #5
  6. Mike C. Fletcher

    Jeff Shannon Guest

    Grant Edwards wrote:

    >On 2004-12-23, Steven Bethard <> 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
     
    Jeff Shannon, Dec 23, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ishwor

    list IndexError

    Ishwor, Dec 22, 2004, in forum: Python
    Replies:
    6
    Views:
    426
    Steven Bethard
    Dec 23, 2004
  2. Ishwor

    Re: list IndexError

    Ishwor, Dec 22, 2004, in forum: Python
    Replies:
    4
    Views:
    309
    Steve Holden
    Dec 23, 2004
  3. Ishwor

    Re: list IndexError

    Ishwor, Dec 23, 2004, in forum: Python
    Replies:
    2
    Views:
    289
    Alex Martelli
    Dec 27, 2004
  4. Replies:
    5
    Views:
    709
    Steven Bethard
    Mar 31, 2005
  5. Replies:
    36
    Views:
    1,635
    MonkeeSage
    Oct 11, 2006
Loading...

Share This Page