List sequential initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HMS Surprise

    #1

    List sequential initialization

    I thought if I could do this:
    >>a = b = ''
    >>a = 'a'
    >>a
    'a'
    >>b
    ''

    then this would behave similarly:
    >>la = lb = []
    >>la.append('a' )
    >>la
    ['a']
    >>lb
    ['a']


    I thought wrong! But don't know why.

    Inquiring minds want to know.....


    thanx,

    jh

  • =?UTF-8?B?UmVuw6kgRmxlc2NoZW5iZXJn?=

    #2
    Re: List sequential initialization

    HMS Surprise schrieb:
    I thought if I could do this:
    >a = b = ''
    Bind both the names a and b to the same string object.
    >a = 'a'
    Bind the name a to a *new* string object with the value 'a'. This
    replaces the previous binding of the name a.
    >la = lb = []
    Bind both the names la and lb to the same list object.
    >la.append('a ')
    Append the string 'a' to the *existing* list object referenced by the
    name la. This modifies the object that is referenced by the name la (and
    also by the name lb), it does not create a new object. The equivalent to
    what you did with the strings above would have been:

    la = ['a']

    This would have created a new list object and bound the name la to it,
    while the name lb would still reference the other list object you
    created earlier.

    If you want to modify the list referenced by la but not the one
    referenced by lb, you need to actually *copy* the list with one of these
    methods:

    lb = list(la)

    or

    lb = la[:]

    --
    René

    Comment

    • HMS Surprise

      #3
      Re: List sequential initialization

      Thanks for the explaination. It didn't seem natural and from the
      tutorial I read:

      A value can be assigned to several variables simultaneously:
      >>x = y = z = 0 # Zero x, y and z

      Maybe I infer too much....

      thanks again,

      jh


      Comment

      • Chris Mellon

        #4
        Re: List sequential initialization

        On 6/12/07, HMS Surprise <john@datavoice int.comwrote:
        Thanks for the explaination. It didn't seem natural and from the
        tutorial I read:
        >
        A value can be assigned to several variables simultaneously:
        >
        >>x = y = z = 0 # Zero x, y and z
        >
        >
        Maybe I infer too much....
        >
        And yet, your answer is right there.

        "A value can be assigned to several variables simultaneously"

        When you say want a value assigned to several variables, Python
        doesn't assume that you actually mean you want 2 different values
        assigned to them.

        Comment

        • Dave Baum

          #5
          Re: List sequential initialization

          In article <1181670619.086 709.116730@g37g 2000prf.googleg roups.com>,
          HMS Surprise <john@datavoice int.comwrote:
          I thought if I could do this:
          >a = b = ''
          a and b refer to the same object (the empty string)
          >a = 'a'
          You are assigning a new value to a - it now refers to the string 'a',
          while b refers to the same thing it always has (the empty string)
          >a
          'a'
          >b
          ''
          >
          then this would behave similarly:
          >la = lb = []
          la and lb refer to the same object, an empty list
          >la.append('a ')
          You are appending 'a' to the list that la refers to.
          >la
          ['a']
          >lb
          ['a']
          Since lb referred to the same list as la, when you modified the list via
          la.append, those changes can also be seen via lb.

          If instead of la.append('a'), you had done:

          la = ['a']

          Then it would have behaved similarly to the first example, and lb would
          still refer to an empty list.
          >
          I thought wrong! But don't know why.
          For immutable objects (such as integers, strings, and tuples), the
          distinction between pointing to the same object or identical copies
          isn't important since you cannot modify the objects. However, when you
          use mutable objects (such as lists) and modify them, then it is
          important to understand when you are dealing with the same object and
          when you are copying the object.

          Assignment makes a name refer to an object. Multiple names can refer to
          the same object (which is what a=b=c does). If you want to make a copy
          of the object, you need to do so explicitly:
          >>a = [1, 2, 3]
          >>b = list(a)
          >>a.append(4)
          >>a
          [1, 2, 3, 4]
          >>b
          [1, 2, 3]


          Dave

          Comment

          • Steve Holden

            #6
            Re: List sequential initialization

            Chris Mellon wrote:
            On 6/12/07, HMS Surprise <john@datavoice int.comwrote:
            >Thanks for the explaination. It didn't seem natural and from the
            >tutorial I read:
            >>
            > A value can be assigned to several variables simultaneously:
            >>
            > >>x = y = z = 0 # Zero x, y and z
            >>
            >>
            >Maybe I infer too much....
            >>
            >
            And yet, your answer is right there.
            >
            "A value can be assigned to several variables simultaneously"
            >
            When you say want a value assigned to several variables, Python
            doesn't assume that you actually mean you want 2 different values
            assigned to them.
            The crucial difference between

            a = b = "ab"
            a = "a"

            and

            a = b = ['a', 'b']
            a.append('c')

            is that in the first case two names are bound to the immutable object
            "ab". Then the first name is rebound to a different immutable object.

            In the second example, both names are bound to the same mutable object,
            a list. That object is then modified. The modification can be performed
            using either name, and both names continue to point to the same (but now
            mutated) object.

            regards
            Steve
            --
            Steve Holden +1 571 484 6266 +1 800 494 3119
            Holden Web LLC/Ltd http://www.holdenweb.com
            Skype: holdenweb http://del.icio.us/steve.holden
            --------------- Asciimercial ------------------
            Get on the web: Blog, lens and tag the Internet
            Many services currently offer free registration
            ----------- Thank You for Reading -------------

            Comment

            Working...