[Tutor] interpreting error messages
Jacob S.
keridee at jayco.net
Thu Oct 7 00:21:55 CEST 2004
Hey,
Just in case you're wondering, the examples you gave follow these
ideas...
>For example, what do the following errors refer to:
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
> TypeError: iteration over non-sequence
A few examples of type errors are:
>>> a = '2'
>>> b = 3
>>> print a+b
Traceback (most recent call last):
File "<pyshell#2>", line 1, in ?
print a+b
TypeError: cannot concatenate 'str' and 'int' objects
>>>
This means you have tried to add a string and and integer.
----------------------------------------------------------------------------
------------
>>> a = "".join([2,4,3,1,5])
Traceback (most recent call last):
File "<pyshell#8>", line 1, in ?
a = "".join([2,4,3,1,5])
TypeError: sequence item 0: expected string, int found
>>>
This type error denotes that you have wrongly used types in joining a list
of *strings*.
----------------------------------------------------------------------------
------------
>>> def foo():
pass
>>> for x in foo:
print x
Traceback (most recent call last):
File "<pyshell#15>", line 1, in ?
for x in foo:
TypeError: iteration over non-sequence
>>>
This means that you are trying to loop through an object that absolutely
cannot be expressed as a list. For example, lists can be turned into lists,
tuples to lists, strings to lists, etc. Dictionaries, classes, functions,
etc. cannot be turned into lists.
----------------------------------------------------------------------------
------------
>>> "t"-6
Traceback (most recent call last):
File "<pyshell#17>", line 1, in ?
"t"-6
TypeError: unsupported operand type(s) for -: 'str' and 'int'
>>>
This type error means that you have tried to do something that just can't
happen between types.
----------------------------------------------------------------------------
------------
>>> del a
>>> a
Traceback (most recent call last):
File "<pyshell#22>", line 1, in ?
a
NameError: name 'a' is not defined
>>>
Name errors occur when you have referenced an object that doesn't exist.
----------------------------------------------------------------------------
---------------
>>> import foo
Traceback (most recent call last):
File "<pyshell#26>", line 1, in ?
import foo
ImportError: No module named foo
>>>
Import errors are obvious. They are raised when the interpreter can't find
the module specified. The module might not be on sys.path, or it might not
exist at all.
----------------------------------------------------------------------------
------------------
>>> a = '2314'
>>> a.write()
Traceback (most recent call last):
File "<pyshell#30>", line 1, in ?
a.write()
AttributeError: 'str' object has no attribute 'write'
>>>
Attribute errors are raised when you have tried to reference an attribute
that doesn't exist.
Another example:
>>> import math
>>> math.sin(math.pi)
1.2246063538223773e-016
>>> math.e
2.7182818284590451
>>> math.pi
3.1415926535897931
>>> math.phi
Traceback (most recent call last):
File "<pyshell#37>", line 1, in ?
math.phi
AttributeError: 'module' object has no attribute 'phi'
>>>
In this case, we imported math, and later found that math doesn't have the
attribute phi -- in other words, phi was not defined inside of this module.
----------------------------------------------------------------------------
------------------
Oh my! My simple boredom caused me to go off subject and start an error
description page myself. I need to go home and get some sleep. If this helps
you in any way, it will be a miracle.
Hope you can find a more complete listing of errors to help you.
Jacob Schmidt
More information about the Tutor
mailing list