append on lists

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

    #16
    Re: append on lists

    John Machin wrote:
    On Sep 16, 6:45 am, Armin <a...@nospam.or gwrote:
    >
    >Yes, but this is very unconvenient.
    >If d should reference the list a extended with a single list element
    >you need at least two lines
    >>
    >a.append(7)
    >d=a
    >>
    >and not more intuitive d = a.append(7)
    >
    Methods/functions which return a value other than the formal None and
    also mutate their environment are "a snare and a delusion". Don't wish
    for them.


    c = [9,10]
    [1,2,3,4,7].append(c) -Is this a valid expression?

    The 'value' of that expression is None.

    However ... that's the way of the implementation of the append method.
    It's a little bit confusing to me ...

    --Armin

    Thanks to all !

    >
    Inconvenient? How often do you want to mutate a list and then set up
    another reference to it?
    >

    Comment

    • Chris Rebert

      #17
      Re: append on lists

      On Tue, Sep 16, 2008 at 1:20 AM, Armin <a@nospam.orgwr ote:
      John Machin wrote:
      >>
      >On Sep 16, 6:45 am, Armin <a...@nospam.or gwrote:
      >>
      >>Yes, but this is very unconvenient.
      >>If d should reference the list a extended with a single list element
      >>you need at least two lines
      >>>
      >>a.append(7)
      >>d=a
      >>>
      >>and not more intuitive d = a.append(7)
      >>
      >Methods/functions which return a value other than the formal None and
      >also mutate their environment are "a snare and a delusion". Don't wish
      >for them.
      >
      >
      >
      c = [9,10]
      [1,2,3,4,7].append(c) -Is this a valid expression?
      Literally, no, because you can't call methods on literals.
      However, the sentiment is valid, though probably not what you want:
      >>c = [9,10]
      >>a = [1,2,3,4,7]
      >>b = a[:]
      >>a.append(c)
      >>a #note the nested list
      [1, 2, 3, 4, 7, [9, 10]]
      >>b
      [1, 2, 3, 4, 7]
      >>b.extend(c)
      >>b
      [1, 2, 3, 4, 7, 9, 10]

      Regards,
      Chris
      >
      The 'value' of that expression is None.
      >
      However ... that's the way of the implementation of the append method.
      It's a little bit confusing to me ...
      >
      --Armin
      >
      Thanks to all !
      >
      >
      >>
      >Inconvenient ? How often do you want to mutate a list and then set up
      >another reference to it?
      >>
      --

      >


      --
      Follow the path of the Iguana...

      Comment

      • Hrvoje Niksic

        #18
        Re: append on lists

        "Chris Rebert" <clp@rebertia.c omwrites:
        > c = [9,10]
        > [1,2,3,4,7].append(c) -Is this a valid expression?
        >
        Literally, no, because you can't call methods on literals.
        This is in fact not true. [1,2,3,4,7].append([9, 10]) is a perfectly
        valid expression, only it doesn't do much (that you can observe).

        The canonical response (no doubt already quoted in this thread) is
        that returning the list from append would confuse the hell out of
        people who expect a copy of the list, such as:

        a = [1, 2, 3]
        b = a.append(4)
        # if the above "worked" in the sense that b == [1, 2, 3, 4], I for one
        # would expect a to remain unchanged

        Comment

        • Duncan Booth

          #19
          Re: append on lists

          "Chris Rebert" <clp@rebertia.c omwrote:
          On Tue, Sep 16, 2008 at 1:20 AM, Armin <a@nospam.orgwr ote:
          > [1,2,3,4,7].append(c) -Is this a valid expression?
          >
          Literally, no, because you can't call methods on literals.
          Rubbish. There is no restriction about calling methods on literals. That
          expression is perfectly valid but has no practical use that I can see.

          There is a syntax gotcha which you may have been thinking of: to call a
          method on an integer literal (or indeed to access any attribute) you have
          to use whitespace between the literal and the dot otherwise you have a
          float literal and a syntax error.
          >>5 .__hex__()
          '0x5'

          The only relatively common use I can think of where you might want to call
          a method directly on a literal is to produce a list of strings while being
          lazy about the typing:

          COLOURS = "red green blue pink yellow".split()

          versus

          COLOURS = ["red", "green", "blue", "pink", "yellow"]


          --
          Duncan Booth http://kupuguy.blogspot.com

          Comment

          • Hrvoje Niksic

            #20
            Re: append on lists

            Duncan Booth <duncan.booth@i nvalid.invalidw rites:
            The only relatively common use I can think of where you might want to call
            a method directly on a literal is to produce a list of strings while being
            lazy about the typing:
            By far the most common is probably 'sep'.join(iter able).

            Comment

            • Alex Marandon

              #21
              Re: append on lists

              Armin wrote:
              Chris Rebert wrote:
              >On Mon, Sep 15, 2008 at 1:24 PM, Armin <a@nospam.orgwr ote:
              >>>
              >>Hi,
              >>>
              >>just a dumb question.
              >>>
              >>Let a = [1,2,3,4,5]
              >>>
              >>Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
              >>
              >Because .append() mutates 'a' and appends the item in-place rather
              >than creating and returning a new list with the item appended, and
              >it's good Python style for mutating methods to have no return value
              >(since all functions must have some return value, Python uses None
              >when the function doesn't explicitly return anything).
              >
              Yes, but this is very unconvenient.
              Hi,

              You might be interested in using the + operator instead of append. You
              could also define your own list type, based on the UserList included in
              the standard library.
              >>from UserList import UserList
              >>class MyList(UserList ):
              .... def my_append(self, value):
              .... return self + [value]
              ....
              >>l = MyList([1,2,3,4])
              >>l
              [1, 2, 3, 4]
              >>l.my_append(5 )
              [1, 2, 3, 4, 5]
              >>l
              [1, 2, 3, 4]

              Comment

              • Armin

                #22
                Re: append on lists

                Duncan Booth wrote:
                "Chris Rebert" <clp@rebertia.c omwrote:
                >On Tue, Sep 16, 2008 at 1:20 AM, Armin <a@nospam.orgwr ote:
                >> [1,2,3,4,7].append(c) -Is this a valid expression?
                >Literally, no, because you can't call methods on literals.
                >
                Rubbish. There is no restriction about calling methods on literals. That
                expression is perfectly valid but has no practical use that I can see.
                The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
                (with c = [8,9]) is identical, but the first expression doesn't provide
                a value. Strange by design ...

                --Armin

                >
                There is a syntax gotcha which you may have been thinking of: to call a
                method on an integer literal (or indeed to access any attribute) you have
                to use whitespace between the literal and the dot otherwise you have a
                float literal and a syntax error.
                >
                >>>5 .__hex__()
                '0x5'
                >
                The only relatively common use I can think of where you might want to call
                a method directly on a literal is to produce a list of strings while being
                lazy about the typing:
                >
                COLOURS = "red green blue pink yellow".split()
                >
                versus
                >
                COLOURS = ["red", "green", "blue", "pink", "yellow"]
                >
                >

                Comment

                • Alex Marandon

                  #23
                  Re: append on lists

                  Armin wrote:
                  Duncan Booth wrote:
                  >
                  The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
                  (with c = [8,9]) is identical,
                  No it's not, + doesn't alter its operands.
                  >>a = 1
                  >>b = 2
                  >>a + b
                  3
                  >>a
                  1

                  Were you expecting a == 3?

                  Comment

                  • Armin

                    #24
                    Re: append on lists

                    Alex Marandon wrote:
                    Armin wrote:
                    >Duncan Booth wrote:
                    >>
                    >The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
                    >(with c = [8,9]) is identical,
                    >
                    No it's not, + doesn't alter its operands.
                    >
                    >>a = 1
                    >>b = 2
                    >>a + b
                    3
                    That's not the point :)

                    What's the value of 1.add(b)? None? Or 3 ??
                    (if add works in the same way as append)
                    a + b doesn't change a,b ... but a.add(b) -a=3

                    Comment

                    • Hrvoje Niksic

                      #25
                      Re: append on lists

                      Armin <a@nospam.orgwr ites:
                      What's the value of 1.add(b)? None? Or 3 ??
                      (if add works in the same way as append)
                      That's exactly the point -- __add__ doesn't work as append. append is
                      a "destructiv e" operation, that mutates an existing object, whereas
                      __add__ returns a different object to be used as the "result" of the
                      addition. This is why append doesn't need to return anything, while
                      __add__ must return the new object.

                      The distinction is even more apparent with sort and sorted, which are
                      destructive and non-destructive aspects of the same operation:
                      >>a = [3, 2, 1]
                      >>a.sort() # destructive (mutates object), so no return value
                      >>a
                      [1, 2, 3]
                      >>a = [3, 2, 1]
                      >>b = sorted(a) # non-destructive (sorts a copy), returns the sorted copy
                      >>a
                      [3, 2, 1]
                      >>b
                      [1, 2, 3]

                      Comment

                      • Maric Michaud

                        #26
                        Re: append on lists

                        Le Tuesday 16 September 2008 14:23:25 Armin, vous avez écrit :
                        Alex Marandon wrote:
                        Armin wrote:
                        Duncan Booth wrote:
                        >
                        The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
                        (with c = [8,9]) is identical,
                        No it's not, + doesn't alter its operands.
                        >>a = 1
                        >>b = 2
                        >>a + b
                        3
                        >
                        That's not the point :)
                        It is, please try to understand it, in python all expressions that mutate an
                        object should return None, it's the case for

                        l.append(x)
                        l.sort()
                        l.reverse()

                        ...

                        all expressions that return something, return a new object, it's the case for

                        1+2
                        1.__add__(2) (which is the same)
                        sorted(l)
                        l[i:j]
                        etc...


                        there are some noticeable exceptions :

                        For coding facilities, some APIs could return the modified part of the object,
                        ex : it = c.pop()

                        Returning the modifyied object itself is mostly considered bad style, because
                        it doesn't make clear if this the object or a copy. There is OTHO a use case
                        for this, for APIs that allow chaining of operations (don't find by memory
                        any example in stdlib, though), alike the ">>" operator in C++, but pyparsing
                        is a good example if I remember well. BTW, Pythoneers are not very fond of
                        that.

                        Finally, the very special case of augmented assignment operators :
                        x.__iadd__(y) which actually return the modified object, but this expression
                        isn't intended to be use by callers, refer to the manual to see a good
                        example of this, how one should implement __add__ and __iadd__ methods.

                        a += b
                        a = a + b

                        both are statments (doesn't evaluate to any object), but are not equivalentif
                        a is mutable.
                        >
                        What's the value of 1.add(b)? None? Or 3 ??
                        (if add works in the same way as append)
                        a + b doesn't change a,b ... but a.add(b) -a=3
                        --
                        http://mail.python.org/mailman/listinfo/python-list


                        --
                        _____________

                        Maric Michaud

                        Comment

                        • Armin

                          #27
                          Re: append on lists

                          Maric Michaud wrote:
                          Le Tuesday 16 September 2008 14:23:25 Armin, vous avez écrit :
                          >Alex Marandon wrote:
                          >>Armin wrote:
                          >>>Duncan Booth wrote:
                          >>>>
                          >>>The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
                          >>>(with c = [8,9]) is identical,
                          >>No it's not, + doesn't alter its operands.
                          >>>
                          >> >>a = 1
                          >> >>b = 2
                          >> >>a + b
                          >>>
                          >>3
                          >That's not the point :)
                          >
                          It is, please try to understand it, in python all expressions that mutate an
                          object should return None, it's the case for
                          >
                          l.append(x)
                          l.sort()
                          l.reverse()
                          >
                          all expressions that return something, return a new object, it's the case for
                          >
                          1+2
                          1.__add__(2) (which is the same)
                          sorted(l)
                          l[i:j]
                          etc...
                          >
                          there are some noticeable exceptions :
                          >
                          For coding facilities, some APIs could return the modified part of the object,
                          ex : it = c.pop()
                          >
                          Returning the modifyied object itself is mostly considered bad style, because
                          it doesn't make clear if this the object or a copy.
                          OK ... That's a good point. Thanks !

                          --Armin

                          Comment

                          • Grant Edwards

                            #28
                            Re: append on lists

                            On 2008-09-16, Armin <a@nospam.orgwr ote:
                            John Machin wrote:
                            >On Sep 16, 6:45 am, Armin <a...@nospam.or gwrote:
                            >>
                            >>Yes, but this is very unconvenient.
                            >>If d should reference the list a extended with a single list element
                            >>you need at least two lines
                            >>>
                            >>a.append(7)
                            >>d=a
                            >>>
                            >>and not more intuitive d = a.append(7)
                            >>
                            >Methods/functions which return a value other than the formal None and
                            >also mutate their environment are "a snare and a delusion". Don't wish
                            >for them.
                            >
                            >
                            >
                            c = [9,10]
                            [1,2,3,4,7].append(c) -Is this a valid expression?
                            Yes.
                            The 'value' of that expression is None.
                            Correct.
                            However ... that's the way of the implementation of the
                            append method. It's a little bit confusing to me ...
                            No, that's a way of calling the append method of a list. The
                            fact that you don't have a name bound to that list doesn't
                            change the fact that the list is mutable and you appended
                            [9,10] to it.

                            --
                            Grant Edwards grante Yow! Are the STEWED PRUNES
                            at still in the HAIR DRYER?
                            visi.com

                            Comment

                            • Grant Edwards

                              #29
                              Re: append on lists

                              On 2008-09-16, Armin <a@nospam.orgwr ote:
                              Duncan Booth wrote:
                              >"Chris Rebert" <clp@rebertia.c omwrote:
                              >>On Tue, Sep 16, 2008 at 1:20 AM, Armin <a@nospam.orgwr ote:
                              >>> [1,2,3,4,7].append(c) -Is this a valid expression?
                              >>Literally, no, because you can't call methods on literals.
                              >>
                              >Rubbish. There is no restriction about calling methods on literals. That
                              >expression is perfectly valid but has no practical use that I can see.
                              >
                              The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
                              (with c = [8,9]) is identical,
                              No, they're not:
                              >>a=[1,2,3,4,7]
                              >>c=[9,10]
                              >>a.append(c)
                              >>a
                              [1, 2, 3, 4, 7, [9, 10]]

                              >>a=[1,2,3,4,7]
                              >>c=[9,10]
                              >>a+c
                              [1, 2, 3, 4, 7, 9, 10]

                              You really ought to install a copy of Python so you can try out
                              things and see how they really work.
                              but the first expression doesn't provide a value. Strange by
                              design ...
                              Not really. You just need to make a little effort to
                              understand the reasoning (which has been explained to you)
                              behind Python's design decision.

                              --
                              Grant Edwards grante Yow! Somewhere in DOWNTOWN
                              at BURBANK a prostitute is
                              visi.com OVERCOOKING a LAMB CHOP!!

                              Comment

                              • Grant Edwards

                                #30
                                Re: append on lists

                                On 2008-09-16, Armin <a@nospam.orgwr ote:
                                Alex Marandon wrote:
                                >Armin wrote:
                                >>Duncan Booth wrote:
                                >>>
                                >>The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
                                >>(with c = [8,9]) is identical,
                                >>
                                >No it's not, + doesn't alter its operands.
                                >>
                                > >>a = 1
                                > >>b = 2
                                > >>a + b
                                >3
                                >
                                That's not the point :)
                                >
                                What's the value of 1.add(b)? None? Or 3 ??
                                >>a = 1
                                >>b = 2
                                >>1.add(b)
                                File "<stdin>", line 1
                                1.add(b)
                                ^
                                SyntaxError: invalid syntax
                                (if add works in the same way as append)
                                It doesn't.
                                a + b doesn't change a,b ... but a.add(b) -a=3
                                WTH are you talking about?

                                --
                                Grant Edwards grante Yow! I just got my PRINCE
                                at bumper sticker ... But now
                                visi.com I can't remember WHO he
                                is ...

                                Comment

                                Working...