Odd Errors

N

Nathan Seese

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'
 
A

alex23

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)
 
A

Aaron \Castironpi\ Brady

The problem is with this:


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!"?
 
A

alex23

Aaron \"Castironpi\" Brady said:
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!"
 
S

Steven D'Aprano

In message


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".
 
L

Lawrence D'Oliveiro

Steven said:
"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.
 
A

Aaron \Castironpi\ Brady

"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?
 
L

Lawrence D'Oliveiro

In message
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.
 
S

Steven D'Aprano

"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)
 
G

greg

Steven said:
"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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top