Odd Errors

Discussion in 'Python' started by Nathan Seese, Sep 29, 2008.

  1. Nathan Seese

    Nathan Seese Guest

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

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

    I get:
    Traceback (most recent call last):
    File "./foobar.py", line 7, in <module>
    lines = lines.append(inLine)
    AttributeError: 'NoneType' object has no attribute 'append'
     
    Nathan Seese, Sep 29, 2008
    #1
    1. Advertising

  2. Nathan Seese

    alex23 Guest

    The problem is with this:

    >         lines = lines.append(inLine)


    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(inLine)
     
    alex23, Sep 29, 2008
    #2
    1. Advertising

  3. On Sep 28, 7:13 pm, alex23 <> wrote:
    > The problem is with this:
    >
    > >         lines = lines.append(inLine)

    >
    > 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(inLine)


    Do you ever want to scream from the rooftops, "'append' operates by
    side-effect!"?
     
    Aaron \Castironpi\ Brady, Sep 29, 2008
    #3
  4. Nathan Seese

    alex23 Guest

    "Aaron \"Castironpi\" Brady" <> wrote:
    > 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!"
     
    alex23, Sep 29, 2008
    #4
  5. Nathan Seese

    Nathan Seese Guest

    > On Sep 28, 7:13 pm, alex23 <> wrote:
    >> The problem is with this:
    >>
    >> >         lines = lines.append(inLine)

    >>
    >> 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(inLine)

    >
    > Do you ever want to scream from the rooftops, "'append' operates by
    > side-effect!"?


    Ah! Thanks, especially for the quick response.
     
    Nathan Seese, Sep 29, 2008
    #5
  6. In message
    <>,
    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.
     
    Lawrence D'Oliveiro, Oct 1, 2008
    #6
  7. On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:

    > In message
    > <>,
    > 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
     
    Steven D'Aprano, Oct 1, 2008
    #7
  8. In message <>, Steven
    D'Aprano wrote:

    > On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:
    >
    >> In message
    >> <>,
    >> 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>.append" isn't a function, it's a procedure. Hence the
    modification of the list is the primary effect, not a side effect.
     
    Lawrence D'Oliveiro, Oct 2, 2008
    #8
  9. On Oct 2, 12:52 am, Lawrence D'Oliveiro <l...@geek-
    central.gen.new_zealand> wrote:
    > In message <>, Steven
    >
    > D'Aprano wrote:
    > > On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:

    >
    > >> In message
    > >> <>,
    > >> 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>.append" 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?
     
    Aaron \Castironpi\ Brady, Oct 2, 2008
    #9
  10. In message
    <>,
    Aaron "Castironpi" Brady wrote:

    > On Oct 2, 12:52 am, Lawrence D'Oliveiro <l...@geek-
    > central.gen.new_zealand> wrote:
    >
    >> In message <>,
    >> Steven
    >>
    >> D'Aprano wrote:
    >> > On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:

    >>
    >> >> In message
    >> >> <>,
    >> >> 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>.append" 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.
     
    Lawrence D'Oliveiro, Oct 2, 2008
    #10
  11. On Thu, 02 Oct 2008 18:52:58 +1300, Lawrence D'Oliveiro wrote:

    > In message <>,
    > Steven D'Aprano wrote:
    >
    >> On Wed, 01 Oct 2008 22:14:49 +1300, Lawrence D'Oliveiro wrote:
    >>
    >>> In message
    >>> <>,
    >>> 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."

    http://lib.store.yahoo.net/lib/paulgraham/acl2.txt

    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>.append" 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
     
    Steven D'Aprano, Oct 2, 2008
    #11
  12. Nathan Seese

    greg Guest

    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
     
    greg, Oct 3, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    986
    Mark Goldin
    Jan 17, 2004
  2. SenthilVel
    Replies:
    0
    Views:
    962
    SenthilVel
    Jun 7, 2006
  3. Jeff Blaine

    Odd build errors

    Jeff Blaine, Oct 30, 2006, in forum: Python
    Replies:
    0
    Views:
    359
    Jeff Blaine
    Oct 30, 2006
  4. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,137
    Richard Heathfield
    Feb 18, 2007
  5. Bakes

    Odd/Weird errors with FTPLib

    Bakes, Sep 13, 2009, in forum: Python
    Replies:
    6
    Views:
    403
    Bakes
    Sep 15, 2009
Loading...

Share This Page