assignment statements: lists vs. strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Klaus Neuner

    assignment statements: lists vs. strings

    Hello,

    I would like to understand the reason for the following difference
    between dealing with lists and dealing with strings: What is this
    difference good for? How it is accounted for in Python slang?

    Klaus
    [color=blue][color=green][color=darkred]
    >>> string1 = "bla"
    >>> string2 = string1
    >>> string1 = string1 + "bla"
    >>> string1[/color][/color][/color]
    'blabla'[color=blue][color=green][color=darkred]
    >>> string2[/color][/color][/color]
    'bla'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> list1 = [1,2]
    >>> list2 = list1
    >>> list1.append(1)
    >>> list1[/color][/color][/color]
    [1, 2, 1][color=blue][color=green][color=darkred]
    >>> list2[/color][/color][/color]
    [1, 2, 1][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
  • Duncan Booth

    #2
    Re: assignment statements: lists vs. strings

    klaus_neuner82@ yahoo.de (Klaus Neuner) wrote in
    news:3e96ebd7.0 402020750.3b0b9 b82@posting.goo gle.com:
    [color=blue]
    > Hello,
    >
    > I would like to understand the reason for the following difference
    > between dealing with lists and dealing with strings: What is this
    > difference good for? How it is accounted for in Python slang?
    >
    > Klaus
    >[color=green][color=darkred]
    >>>> string1 = "bla"
    >>>> string2 = string1
    >>>> string1 = string1 + "bla"
    >>>> string1[/color][/color]
    > 'blabla'[color=green][color=darkred]
    >>>> string2[/color][/color]
    > 'bla'[color=green][color=darkred]
    >>>>[/color][/color]
    >[color=green][color=darkred]
    >>>> list1 = [1,2]
    >>>> list2 = list1
    >>>> list1.append(1)
    >>>> list1[/color][/color]
    > [1, 2, 1][color=green][color=darkred]
    >>>> list2[/color][/color]
    > [1, 2, 1][color=green][color=darkred]
    >>>>[/color][/color]
    >[/color]

    You aren't comparing like with like here. You can do exactly the same as
    your string example using a list:
    [color=blue][color=green][color=darkred]
    >>> list1 = [1,2]
    >>> list2 = list1
    >>> list1 = list1 + [3,4]
    >>> list1[/color][/color][/color]
    [1, 2, 3, 4][color=blue][color=green][color=darkred]
    >>> list2[/color][/color][/color]
    [1, 2][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    The difference is that addition of strings, or lists, or anything else,
    creates a new object. The append method of the list type modifies an
    existing object. The string types don't have an append method because they
    are IMMUTABLE: immutable objects, once created never change their values.
    Mutable objects can change their values.

    Remember that assignment always creates another reference to an existing
    object, it never makes a copy of an object.

    Builtin immutable objects include: int, long, float, str, unicode, tuple
    Builtin mutable objects include: list, dict

    Comment

    • Dang Griffith

      #3
      Re: assignment statements: lists vs. strings

      On 2 Feb 2004 07:50:01 -0800, klaus_neuner82@ yahoo.de (Klaus Neuner)
      wrote:
      [color=blue]
      >Hello,
      >
      >I would like to understand the reason for the following difference
      >between dealing with lists and dealing with strings: What is this
      >difference good for? How it is accounted for in Python slang?
      >
      >Klaus
      >[color=green][color=darkred]
      >>>> string1 = "bla"
      >>>> string2 = string1
      >>>> string1 = string1 + "bla"
      >>>> string1[/color][/color]
      >'blabla'[color=green][color=darkred]
      >>>> string2[/color][/color]
      >'bla'[color=green][color=darkred]
      >>>>[/color][/color]
      >[color=green][color=darkred]
      >>>> list1 = [1,2]
      >>>> list2 = list1
      >>>> list1.append(1)
      >>>> list1[/color][/color]
      >[1, 2, 1][color=green][color=darkred]
      >>>> list2[/color][/color]
      >[1, 2, 1][color=green][color=darkred]
      >>>>[/color][/color][/color]
      Strings are immutable, lists are mutable. String concatenation builds
      a new string object. List appending does not build a new list.
      Variables/names are references to objects; they are not themselves the
      objects.

      In the first example, you point both variables/names to the "bla"
      string. You then build a new string object "blabla" and point the
      string1 variable to that new string. string2 still points to the old
      one.

      In the second example, you point two variables/names to the [1, 2]
      list. You then append an item to that list. The two variables still
      point to the same list, which now has an additional element.

      The "what is it good for" is the mutable/immutable difference. Think
      of it this way--numbers are immutable. If you said:[color=blue][color=green][color=darkred]
      >>> n1 = 3
      >>> n2 = n1
      >>> n1 = n1 + 1
      >>> print n1, n2[/color][/color][/color]
      You wouldn't expect the number 3 itself to change. When you add n1 to
      3, a new object with the value 4 is created and n1 is reassigned to
      point to it. Just as with the string, n2 still points to the "old" 3
      object.
      --dang
      p.s.
      This last bit about the numbers is supposed to be conceptual, rather
      than a description of the internal implementation.

      Comment

      • Terry Reedy

        #4
        Re: assignment statements: lists vs. strings


        "Klaus Neuner" <klaus_neuner82 @yahoo.de> wrote in message
        news:3e96ebd7.0 402020750.3b0b9 b82@posting.goo gle.com...[color=blue]
        > Hello,
        >
        > I would like to understand the reason for the following difference
        > between dealing with lists and dealing with strings:[/color]

        You are doing different operations, and hence should not expect the same
        result.

        [color=blue][color=green][color=darkred]
        > >>> string1 = "bla"
        > >>> string2 = string1
        > >>> string1 = string1 + "bla"[/color][/color][/color]

        Here you add two strings together to get a third, which you bind back to
        the first name
        [color=blue][color=green][color=darkred]
        > >>> string1[/color][/color]
        > 'blabla'[color=green][color=darkred]
        > >>> string2[/color][/color]
        > 'bla'[/color]
        [color=blue][color=green][color=darkred]
        > >>> list1 = [1,2]
        > >>> list2 = list1
        > >>> list1.append(1)[/color][/color][/color]

        Here you mutate a list in place, which you can do because lists are mutable
        and which you cannot do with strings. To parallel your string operation,
        try

        list1 = list1 + [1,2]

        before you mutate list1 and you should get [1,2,1,2]

        [color=blue][color=green][color=darkred]
        > >>> list1[/color][/color]
        > [1, 2, 1][color=green][color=darkred]
        > >>> list2[/color][/color]
        > [1, 2, 1][/color]

        Terry J. Reedy




        Comment

        • Aahz

          #5
          Re: assignment statements: lists vs. strings

          In article <3e96ebd7.04020 20750.3b0b9b82@ posting.google. com>,
          Klaus Neuner <klaus_neuner82 @yahoo.de> wrote:[color=blue]
          >
          >I would like to understand the reason for the following difference
          >between dealing with lists and dealing with strings: What is this
          >difference good for? How it is accounted for in Python slang?[/color]


          --
          Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

          "The joy of coding Python should be in seeing short, concise, readable
          classes that express a lot of action in a small amount of clear code --
          not in reams of trivial code that bores the reader to death." --GvR

          Comment

          • Karl Pflästerer

            #6
            Re: assignment statements: lists vs. strings

            On 2 Feb 2004, Klaus Neuner <- klaus_neuner82@ yahoo.de wrote:
            [color=blue]
            > I would like to understand the reason for the following difference
            > between dealing with lists and dealing with strings: What is this
            > difference good for? How it is accounted for in Python slang?[/color]

            Strings are immutable and lists are mutable objects.
            [color=blue][color=green][color=darkred]
            >>>> string1 = "bla"
            >>>> string2 = string1
            >>>> string1 = string1 + "bla"[/color][/color][/color]

            Here you create a new string string1 which is fresh allocated.[color=blue][color=green][color=darkred]
            >>>> string1[/color][/color]
            > 'blabla'[color=green][color=darkred]
            >>>> string2[/color][/color]
            > 'bla'[/color]

            If you did `id(string1)' and `id(string2)' before and after the
            assignment you would have that after the assignment you had two
            different objects.
            [color=blue][color=green][color=darkred]
            >>>> list1 = [1,2]
            >>>> list2 = list1
            >>>> list1.append(1)[/color][/color][/color]

            Here you modify an existing list in place. But that's not the same as
            you did with your string exapmle. With a string you couldn't do that
            since a string is immutable.

            If you had written list1 = list1 + [1] you would have had a new list
            object.
            [color=blue][color=green][color=darkred]
            >>> L1 = [1,2]
            >>> L2 = L1
            >>> id(L1)[/color][/color][/color]
            10489068[color=blue][color=green][color=darkred]
            >>> id(L2)[/color][/color][/color]
            10489068[color=blue][color=green][color=darkred]
            >>> L1 = L1 + [1]
            >>> id(L1)[/color][/color][/color]
            10488140[color=blue][color=green][color=darkred]
            >>> id(L2)[/color][/color][/color]
            10489068[color=blue][color=green][color=darkred]
            >>> L2[/color][/color][/color]
            [1, 2][color=blue][color=green][color=darkred]
            >>> L1[/color][/color][/color]
            [1, 2, 1][color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]


            KP

            --
            If you have nothing to say on a subject, replying with a line such as,
            "I agree with this." puts you in the TO:'s for all future messages, and
            establishes you as "one who really cares", if not an actual expert, on
            the topic at hand. -- from the Symbolics Guidelines for Sending Mail

            Comment

            • Dan Bishop

              #7
              Re: assignment statements: lists vs. strings

              klaus_neuner82@ yahoo.de (Klaus Neuner) wrote in message news:<3e96ebd7. 0402020750.3b0b 9b82@posting.go ogle.com>...[color=blue]
              > Hello,
              >
              > I would like to understand the reason for the following difference
              > between dealing with lists and dealing with strings: What is this
              > difference good for? How it is accounted for in Python slang?
              >
              > Klaus
              >[color=green][color=darkred]
              > >>> string1 = "bla"
              > >>> string2 = string1
              > >>> string1 = string1 + "bla"
              > >>> string1[/color][/color]
              > 'blabla'[color=green][color=darkred]
              > >>> string2[/color][/color]
              > 'bla'[color=green][color=darkred]
              > >>>[/color][/color]
              >[color=green][color=darkred]
              > >>> list1 = [1,2]
              > >>> list2 = list1
              > >>> list1.append(1)[/color][/color][/color]

              This is *not* equivalent to the string code. Notice that
              [color=blue][color=green][color=darkred]
              >>> list1 = list1 + [1]
              >>> list1[/color][/color][/color]
              [1, 2, 1][color=blue][color=green][color=darkred]
              >>> list2[/color][/color][/color]
              [1, 2]

              *does* behave like your string example.
              [color=blue][color=green][color=darkred]
              > >>> list1[/color][/color]
              > [1, 2, 1][color=green][color=darkred]
              > >>> list2[/color][/color]
              > [1, 2, 1][/color]

              Unlike string1, the name "list1" hasn't been rebound since the
              assignment "list2 = list1", and therefore they still refer to the same
              object.

              Comment

              • Klaus Neuner

                #8
                Re: assignment statements: lists vs. strings

                Thank you all. I have understood the point now.

                Klaus

                I would like to ask (Duncan) another question, although it is off
                topic: I have seen those small photographs in the header of articles
                quite often. How are they produced?

                Comment

                • Duncan Booth

                  #9
                  Re: assignment statements: lists vs. strings

                  klaus_neuner82@ yahoo.de (Klaus Neuner) wrote in
                  news:3e96ebd7.0 402030333.7c303 cfb@posting.goo gle.com:
                  [color=blue]
                  > Thank you all. I have understood the point now.
                  >
                  > Klaus
                  >
                  > I would like to ask (Duncan) another question, although it is off
                  > topic: I have seen those small photographs in the header of articles
                  > quite often. How are they produced?
                  >[/color]

                  Do a Google search for X-Face. Some newsreaders recognise the X-Face header
                  line as containing a small 48x48 pixel monochrome bitmap. You can download
                  programs to convert from more usual bitmap formats into X-Face format then
                  you just have to include the header line in all your posts; most
                  newsreaders will let you add custom header lines fairly easily.


                  Comment

                  • Klaus Neuner

                    #10
                    Re: assignment statements: lists vs. strings

                    Thanks.

                    Comment

                    Working...