Odd Errors

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

    Odd Errors

    When I run:
    #!/usr/bin/python
    lines = list()

    while 1:
    try:
    inLine = raw_input()
    lines = lines.append(in Line)
    except EOFError:
    break

    I get:
    Traceback (most recent call last):
    File "./foobar.py", line 7, in <module>
    lines = lines.append(in Line)
    AttributeError: 'NoneType' object has no attribute 'append'
  • alex23

    #2
    Re: Odd Errors

    The problem is with this:
            lines = lines.append(in Line)
    The append method of a list modifies the list in-place, it doesn't
    return a copy of the list with the new element appended. In fact, it
    returns None, which it then attaches the label 'lines' to, so the next
    iteration through it tries to call None.append...

    Replace the line with:

    lines.append(in Line)

    Comment

    • Aaron \Castironpi\ Brady

      #3
      Re: Odd Errors

      On Sep 28, 7:13 pm, alex23 <wuwe...@gmail. comwrote:
      The problem is with this:
      >
              lines = lines.append(in Line)
      >
      The append method of a list modifies the list in-place, it doesn't
      return a copy of the list with the new element appended. In fact, it
      returns None, which it then attaches the label 'lines' to, so the next
      iteration through it tries to call None.append...
      >
      Replace the line with:
      >
          lines.append(in Line)
      Do you ever want to scream from the rooftops, "'append' operates by
      side-effect!"?

      Comment

      • alex23

        #4
        Re: Odd Errors

        "Aaron \"Castironpi \" Brady" <castiro...@gma il.comwrote:
        Do you ever want to scream from the rooftops, "'append' operates by
        side-effect!"?
        "I'm mad as hell, and I'm not going to mutate in-place anymore!"

        Comment

        • Nathan Seese

          #5
          Re: Odd Errors

          On Sep 28, 7:13 pm, alex23 <wuwe...@gmail. comwrote:
          >The problem is with this:
          >>
                  lines = lines.append(in Line)
          >>
          >The append method of a list modifies the list in-place, it doesn't
          >return a copy of the list with the new element appended. In fact, it
          >returns None, which it then attaches the label 'lines' to, so the next
          >iteration through it tries to call None.append...
          >>
          >Replace the line with:
          >>
          >    lines.append(in Line)
          >
          Do you ever want to scream from the rooftops, "'append' operates by
          side-effect!"?
          Ah! Thanks, especially for the quick response.

          Comment

          • Lawrence D'Oliveiro

            #6
            Re: Odd Errors

            In message
            <1b1ed34d-f386-4389-a7f1-ce68be4e2a14@k3 0g2000hse.googl egroups.com>,
            Aaron "Castironpi " Brady wrote:
            Do you ever want to scream from the rooftops, "'append' operates by
            side-effect!"?
            No. It's an effect, not a side-effect.

            Comment

            • Steven D'Aprano

              #7
              Re: Odd Errors

              On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:
              In message
              <1b1ed34d-f386-4389-a7f1-ce68be4e2a14@k3 0g2000hse.googl egroups.com>,
              Aaron "Castironpi " Brady wrote:
              >
              >Do you ever want to scream from the rooftops, "'append' operates by
              >side-effect!"?
              >
              No. It's an effect, not a side-effect.
              "Side-effect" has the technical meaning in functional languages of any
              change of state that isn't the creation and return of a function result.

              People who have been influenced by such functional languages, and many
              Python users are, often use the same meaning. I for one have no
              difficulty understanding from context the difference between "append
              operates by side-effect" and "a function which modifies global variables
              is having side-effects".



              --
              Steven

              Comment

              • Lawrence D'Oliveiro

                #8
                Re: Odd Errors

                In message <pan.2008.10.01 .09.44.17@REMOV E.THIS.cybersou rce.com.au>, Steven
                D'Aprano wrote:
                On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:
                >
                >In message
                ><1b1ed34d-f386-4389-a7f1-ce68be4e2a14@k3 0g2000hse.googl egroups.com>,
                >Aaron "Castironpi " Brady wrote:
                >>
                >>Do you ever want to scream from the rooftops, "'append' operates by
                >>side-effect!"?
                >>
                >No. It's an effect, not a side-effect.
                >
                "Side-effect" has the technical meaning in functional languages of any
                change of state that isn't the creation and return of a function result.
                "Side" means that it happens as the by-product of returning a function
                result. "<list>.app end" isn't a function, it's a procedure. Hence the
                modification of the list is the primary effect, not a side effect.

                Comment

                • Aaron \Castironpi\ Brady

                  #9
                  Re: Odd Errors

                  On Oct 2, 12:52 am, Lawrence D'Oliveiro <l...@geek-
                  central.gen.new _zealandwrote:
                  In message <pan.2008.10.01 .09.44...@REMOV E.THIS.cybersou rce.com.au>, Steven
                  >
                  D'Aprano wrote:
                  On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:
                  >
                  In message
                  <1b1ed34d-f386-4389-a7f1-ce68be4e2...@k3 0g2000hse.googl egroups.com>,
                  Aaron "Castironpi " Brady wrote:
                  >
                  >Do you ever want to scream from the rooftops, "'append' operates by
                  >side-effect!"?
                  >
                  No. It's an effect, not a side-effect.
                  >
                  "Side-effect" has the technical meaning in functional languages of any
                  change of state that isn't the creation and return of a function result..
                  >
                  "Side" means that it happens as the by-product of returning a function
                  result. "<list>.app end" isn't a function, it's a procedure. Hence the
                  modification of the list is the primary effect, not a side effect.
                  I was using the technical definition from functional languages, not
                  the literal "per word" definition. Maybe the FL crowd chose their
                  words poorly, who knows?

                  Comment

                  • Lawrence D'Oliveiro

                    #10
                    Re: Odd Errors

                    In message
                    <0a724214-0dea-43ff-947e-9d073581573c@u6 5g2000hsc.googl egroups.com>,
                    Aaron "Castironpi " Brady wrote:
                    On Oct 2, 12:52 am, Lawrence D'Oliveiro <l...@geek-
                    central.gen.new _zealandwrote:
                    >
                    >In message <pan.2008.10.01 .09.44...@REMOV E.THIS.cybersou rce.com.au>,
                    >Steven
                    >>
                    >D'Aprano wrote:
                    On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:
                    >>
                    >In message
                    ><1b1ed34d-f386-4389-a7f1-ce68be4e2...@k3 0g2000hse.googl egroups.com>,
                    >Aaron "Castironpi " Brady wrote:
                    >>
                    >>Do you ever want to scream from the rooftops, "'append' operates by
                    >>side-effect!"?
                    >>
                    >No. It's an effect, not a side-effect.
                    >>
                    "Side-effect" has the technical meaning in functional languages of any
                    change of state that isn't the creation and return of a function
                    result.
                    >>
                    >"Side" means that it happens as the by-product of returning a function
                    >result. "<list>.app end" isn't a function, it's a procedure. Hence the
                    >modification of the list is the primary effect, not a side effect.
                    >
                    I was using the technical definition from functional languages ...
                    Which is where the use of "side" would make sense, given that functional
                    languages are full of functions, not procedures.

                    Comment

                    • Steven D'Aprano

                      #11
                      Re: Odd Errors

                      On Thu, 02 Oct 2008 18:52:58 +1300, Lawrence D'Oliveiro wrote:
                      In message <pan.2008.10.01 .09.44.17@REMOV E.THIS.cybersou rce.com.au>,
                      Steven D'Aprano wrote:
                      >
                      >On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:
                      >>
                      >>In message
                      >><1b1ed34d-f386-4389-a7f1-ce68be4e2a14@k3 0g2000hse.googl egroups.com>,
                      >>Aaron "Castironpi " Brady wrote:
                      >>>
                      >>>Do you ever want to scream from the rooftops, "'append' operates by
                      >>>side-effect!"?
                      >>>
                      >>No. It's an effect, not a side-effect.
                      >>
                      >"Side-effect" has the technical meaning in functional languages of any
                      >change of state that isn't the creation and return of a function
                      >result.
                      >
                      "Side" means that it happens as the by-product of returning a function
                      result.
                      Not in functional programming circles. This is what Paul Graham says
                      about "side-effect" in Lisp:

                      "A side-effect is some change to the state of the world that happens as a
                      consequence of evaluating an expression."



                      It is common to refer to procedures (in languages which have them, like
                      Pascal) as always operating by side-effect. I remember being taught that
                      in Comp Sci classes in the mid 1980s.

                      "<list>.app end" isn't a function, it's a procedure. Hence the
                      modification of the list is the primary effect, not a side effect.
                      Python doesn't have procedures. list.append is a function, and it returns
                      None. That's why hardly a week goes by without somebody failing to read
                      the Fine Manual and being surprised why code like this doesn't work:

                      alist = [1, 2, 3]
                      alist = alist.append(4)
                      print len(alist)



                      --
                      Steven

                      Comment

                      • greg

                        #12
                        Re: Odd Errors

                        Steven D'Aprano wrote:
                        "Side-effect" has the technical meaning in functional languages of any
                        change of state that isn't the creation and return of a function result.
                        Actually, the term has that meaning for all programming
                        languages. The main distinguishing feature of functional
                        languages is that there are *no* side effects.

                        The confusing thing is that in everyday English the term
                        implies something bad or unwanted (e.g. side effects of
                        a drug). That's not necessarily true of the technical
                        meaning -- often the side effect is exactly what we want,
                        as in the case of "append".

                        --
                        Greg

                        Comment

                        Working...