Results 1 to 7 of 7

Thread: Python weird list feature (bug?)

  1. #1
    Join Date
    Jun 2009
    Location
    Land of Paranoia and Guns
    Beans
    194
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Python weird list feature (bug?)

    I've noticed an interesting behavior in python 2.6 with lists.
    If you assign string 'string' to a, then run this code:
    Code:
    a='string'
    b=a
    b=''
    print a
    a will still equal 'string'. However, if a is a list:
    Code:
    a=['item1', 'item2']
    b=a
    del b[0]
    print a
    a will be just ['item2']. While this is rather annoying, it is fortunately easy to work around.

    Thoughts?
    Don't use W3Schools as a resource! (Inconsequential foul language at the jump)
    Open Linux Forums (More foul language, but well worth it for the quality of support and good humor.)
    If you want to discuss W3Schools, please PM me instead of posting.

  2. #2
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Python weird list feature (bug?)

    Why is this surprising? Strings in python are immutable, lists are mutable. This is the expected behaviour.

  3. #3
    Join Date
    Jun 2010
    Location
    UK
    Beans
    206
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Python weird list feature (bug?)

    what you are suggesting is a bug is one of the key principles of python.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Beans
    535
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Python weird list feature (bug?)

    This is common to a lot of scripting languages. Ruby and Javascript have this behaviour too.

    It is because arrays (and most other objects) are implicitly passed around as references in these languages. Numbers and strings are passed around by value for ease of use more than anything else.

    Perl takes a different approach and requires references to be explicit. This makes complex data structures complicated to work with.
    DMedia - Distributed Media Library
    LaVida - A simulation game for Linux
    AskUbuntu

  5. #5
    Join Date
    Jun 2009
    Location
    Land of Paranoia and Guns
    Beans
    194
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Python weird list feature (bug?)

    Thanks for all the prompt replies. The reason I was surprised was that in my experience, none of the other programming languages I've used have done this.

    SOLVED
    Don't use W3Schools as a resource! (Inconsequential foul language at the jump)
    Open Linux Forums (More foul language, but well worth it for the quality of support and good humor.)
    If you want to discuss W3Schools, please PM me instead of posting.

  6. #6
    Join Date
    Sep 2007
    Beans
    163

    Re: Python weird list feature (bug?)

    What you need to do is instead of

    Code:
    b = a
    code it as

    Code:
    b = a[::]
    The second example makes an exact copy of the list object assigned to a and assigns it to b. The first simply assigns both a and b to the same list object in memory.

  7. #7
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python weird list feature (bug?)

    Quote Originally Posted by sh228 View Post
    I've noticed an interesting behavior in python 2.6 with lists.
    If you assign string 'string' to a, then run this code:
    Code:
    a='string'
    b=a
    b=''
    print a
    a will still equal 'string'. However, if a is a list:
    Code:
    a=['item1', 'item2']
    b=a
    del b[0]
    print a
    a will be just ['item2']. While this is rather annoying, it is fortunately easy to work around.

    Thoughts?
    In python, variables are just names for objects, not the objects themselves. If you're familiar with C or C++, you can think of every variable as a pointer to a generic Object type (ignoring the whole virtual mess). Here's what you're doing:

    Code:
    #a = "string"
    #b = a
    a -- "string"
    b --/
    
    # b = ""
    a -- "string"
    b -- ""
    
    # now, a still refers to "string"
    The second example:

    Code:
    # a = [item1, item2]
    # b = a
    a -- [item1, item2]
    b --/
    
    # del b[0]
    a -- [item2]
    b --/
    
    # a still refers to the same, now changed, list
    Last edited by schauerlich; November 5th, 2010 at 07:09 PM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •