开个贴,用于记录平时经常碰到的python的错误同时对导致错误的原因进行分析,并持续更新,方便以后查询,学习。
知识在于积累嘛!微笑
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
错误:
代码如下:
>>> def f(x, y):
print x, y
>>> t = (‘a’, ‘b’)
>>> f(t)
traceback (most recent call last):
file “”, line 1, in
f(t)
typeerror: f() takes exactly 2 arguments (1 given)
【错误分析】不要误以为元祖里有两个参数,将元祖传进去就可以了,实际上元祖作为一个整体只是一个参数,
实际需要两个参数,所以报错。必需再传一个参数方可.
代码如下:
>>> f(t, ‘var2’)
(‘a’, ‘b’) var2
更常用的用法: 在前面加*,代表引用元祖
代码如下:
>>> f(*t)
‘a’, ‘b’
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
错误:
代码如下:
>>> def func(y=2, x):
return x + y
syntaxerror: non-default argument follows default argument
【错误分析】在c++,python中默认参数从左往右防止,而不是相反。这可能跟参数进栈顺序有关。
代码如下:
>>> def func(x, y=2):
return x + y
>>> func(1)
3
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
错误:
代码如下:
>>> d1 = {‘x’:1, ‘y’:2}
>>> d1[‘x’]
1
>>> d1[‘z’]
traceback (most recent call last):
file “”, line 1, in
d1[‘z’]
keyerror: ‘z’
【错误分析】这是python中字典键错误的提示,如果想让程序继续运行,可以用字典中的get方法,如果键存在,则获取该键对应的值,不存在的,返回none,也可打印提示信息.
代码如下:
>>> d1.get(‘z’, ‘key not exist!’)
‘key not exist!’
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
错误:
代码如下:
>>> from math import sqrt
>>> exec “sqrt = 1”
>>> sqrt(4)
traceback (most recent call last):
file “”, line 1, in
sqrt(4)
typeerror: ‘int’ object is not callable
【错误分析】exec语句最有用的地方在于动态地创建代码字符串,但里面存在的潜在的风险,它会执行其他地方的字符串,在cgi中更是如此!比如例子中的sqrt = 1,从而改变了当前的命名空间,从math模块中导入的sqrt不再和函数名绑定而是成为了一个整数。要避免这种情况,可以通过增加in ,其中就是起到放置代码字符串命名空间的字典。
代码如下:
>>> from math import sqrt
>>> scope = {}
>>> exec “sqrt = 1” in scope
>>> sqrt(4)
2.0
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
错误:
代码如下:
>>> seq = [1, 2, 3, 4]
>>> sep = ‘+’
>>> sep.join(seq)
traceback (most recent call last):
file “”, line 1, in
sep.join(seq)
typeerror: sequence item 0: expected string, int found
【错误分析】join是split的逆方法,是非常重要的字符串方法,但不能用来连接整数型列表,所以需要改成:
代码如下:
>>> seq = [‘1’, ‘2’, ‘3’, ‘4’]
>>> sep = ‘+’
>>> sep.join(seq)
‘1+2+3+4’
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
错误:
代码如下:
>>> print r’c:\program files\foo\bar\’
syntaxerror: eol while scanning string literal
【错误分析】python中原始字符串以r开头,里面可以放置任意原始字符,包括\,包含在字符中的\不做转义。
但是,不能放在末尾!也就是说,最后一个字符不能是\,如果真 需要的话,可以这样写:
代码如下:
>>> print r’c:\program files\foo\bar’ “\\”
c:\program files\foo\bar\
>>> print r’c:\program files\foo\bar’ + “\\”
c:\program files\foo\bar\
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码:
代码如下:
bad = ‘bad’
try:
raise bad
except bad:
print ‘got bad!’
错误:
代码如下:
>>>
traceback (most recent call last):
file “d:\learn\python\learn.py”, line 4, in
raise bad
typeerror: exceptions must be old-style classes or derived from baseexception, not str
【错误分析】因所用的python版本2.7,比较高的版本,raise触发的异常,只能是自定义类异常,而不能是字符串。所以会报错,字符串改为自定义类,就可以了。
代码如下:
class bad(exception):
pass
def raiseexception():
raise bad()
try:
raiseexception()
except bad:
print ‘got bad!’
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
class super:
def method(self):
print “super’s method”
class sub(super):
def method(self):
print “sub’s method”
super.method()
print “over…”
s = sub()
s.method()
执行上面一段代码,错误如下:
代码如下:
>>>
sub’s method
traceback (most recent call last):
file “d:\learn\python\test.py”, line 12, in
s.method()
file “d:\learn\python\test.py”, line 8, in method
super.method()
typeerror: unbound method method() must be called with super instance as first argument (got nothing instead)
【错误分析】python中调用类的方法,必须与实例绑定,或者调用自身.
代码如下:
classname.method(x, ‘parm’)
classname.method(self)
所以上面代码,要调用super类的话,只需要加个self参数即可。
代码如下:
class super:
def method(self):
print “super’s method”
class sub(super):
def method(self):
print “sub’s method”
super.method(self)
print “over…”
s = sub()
s.method()
#输出结果
>>>
sub’s method
super’s method
over…
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> reload(sys)
traceback (most recent call last):
file “”, line 1, in
nameerror: name ‘sys’ is not defined
【错误分析】reload期望得到的是对象,所以该模块必须成功导入。在没导入模块前,不能重载.
代码如下:
>>> import sys
>>> reload(sys)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> def f(x, y, z):
return x + y + z
>>> args = (1,2,3)
>>> print f(args)
traceback (most recent call last):
file “”, line 1, in
print f(args)
typeerror: f() takes exactly 3 arguments (1 given)
【错误分析】args是一个元祖,如果是f(args),那么元祖是作为一个整体作为一个参数
*args,才是将元祖中的每个元素作为参数
代码如下:
>>> f(*args)
6
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> def f(a,b,c,d):
… print a,b,c,d
…
>>> args = (1,2,3,4)
>>> f(**args)
traceback (most recent call last):
file “”, line 1, in
typeerror: f() argument after ** must be a mapping, not tuple
【错误分析】错误原因**匹配并收集在字典中所有包含位置的参数,但传递进去的却是个元祖。
所以修改传递参数如下:
代码如下:
>>> args = {‘a’:1,’b’:2,’c’:3}
>>> args[‘d’] = 4
>>> f(**args)
1 2 3 4
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
【错误分析】在函数hider()内使用了内置变量open,但根据python作用域规则legb的优先级:
先是查找本地变量==》模块内的其他函数==》全局变量==》内置变量,查到了即停止查找。
所以open在这里只是个字符串,不能作为打开文件来使用,所以报错,更改变量名即可。
可以导入__builtin__模块看到所有内置变量:异常错误、和内置方法
代码如下:
>>> import __builtin__
>>> dir(__builtin__)
[‘arithmeticerror’, ‘assertionerror’, ‘attributeerror’,..
…………………………………..zip,filter,map]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
in [105]: t1 = (1)
in [106]: t2 = (2,3)
in [107]: t1 + t2
—————————————————————————
typeerror traceback (most recent call last)
in ()
—-> 1 t1 + t2;
typeerror: unsupported operand type(s) for +: ‘int’ and ‘tuple’
【错误分析】(1)的类型是整数,所以不能与另一个元祖做合并操作,如果只有一个元素的元祖,应该用(1,)来表示
代码如下:
in [108]: type(t1)
out[108]: int
in [109]: t1 = (1,)
in [110]: t2 = (2,3)
in [111]: t1 + t2
out[111]: (1, 2, 3)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> hash(1,(2,[3,4]))
traceback (most recent call last):
file “”, line 1, in
hash((1,2,(2,[3,4])))
typeerror: unhashable type: ‘list’
【错误分析】字典中的键必须是不可变对象,如(整数,浮点数,字符串,元祖).
可用hash()判断某个对象是否可哈希
代码如下:
>>> hash(‘string’)
-1542666171
但列表中元素是可变对象,所以是不可哈希的,所以会报上面的错误.
如果要用列表作为字典中的键,最简单的办法是:
代码如下:
>>> d = {}
>>> d[tuple([3,4])] = 5
>>> d
{(3, 4): 5}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> l = [2,1,4,3]
>>> l.reverse().sort()
traceback (most recent call last):
file “”, line 1, in
attributeerror: ‘nonetype’ object has no attribute ‘sort’
>>> l
[3, 4, 1, 2]
【错误分析】列表属于可变对象,其append(),sort(),reverse()会在原处修改对象,不会有返回值,
或者说返回值为空,所以要实现反转并排序,不能并行操作,要分开来写
代码如下:
>>> l = [2,1,4,3]
>>> l.reverse()
>>> l.sort()
>>> l
[1, 2, 3, 4]
或者用下面的方法实现:
代码如下:
in [103]: sorted(reversed([2,1,4,3]))
out[103]: [1, 2, 3, 4]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> class = 78
syntaxerror: invalid syntax
【错误分析】class是python保留字,python保留字不能做变量名,可以用class,或klass
同样,保留字不能作为模块名来导入,比如说,有个and.py,但不能将其作为模块导入
代码如下:
>>> import and
syntaxerror: invalid syntax
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> f = open(‘d:\new\text.data’,’r’)
traceback (most recent call last):
file “”, line 1, in
ioerror: [errno 22] invalid mode (‘r’) or filename: ‘d:\new\text.data’
>>> f = open(r’d:\new\text.data’,’r’)
>>> f.read()
‘very\ngood\naaaaa’
【错误分析】\n默认为换行,\t默认为tab键.
所以在d:\目录下找不到ew目录下的ext.data文件,将其改为raw方式输入即可。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
try:
print 1 / 0
except zeropisionerror:
print ‘integer pision or modulo by zero’
finally:
print ‘done’
else:
print ‘continue handle other part’
报错如下:
d:\>python learn.py
file “learn.py”, line 11
else:
^
syntaxerror: invalid syntax
【错误分析】错误原因,else, finally执行位置;正确的程序应该如下:
代码如下:
try:
print 1 / 0
except zeropisionerror:
print ‘integer pision or modulo by zero’
else:
print ‘continue handle other part’
finally:
print ‘done’
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> [x,y for x in range(2) for y in range(3)]
file “”, line 1
[x,y for x in range(2) for y in range(3)]
^
syntaxerror: invalid syntax
【错误分析】错误原因,列表解析中,x,y必须以数组的方式列出(x,y)
代码如下:
>>> [(x,y) for x in range(2) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class justcounter:
__secretcount = 0
def count(self):
self.__secretcount += 1
print ‘secretcount is:’, self.__secretcount
count1 = justcounter()
count1.count()
count1.count()
count1.__secretcount
报错如下:
代码如下:
>>>
secretcount is: 1
secretcount is: 2
traceback (most recent call last):
file “d:\learn\python\learn.py”, line 13, in
count1.__secretcount
attributeerror: justcounter instance has no attribute ‘__secretcount’
【错误分析】双下划线的类属性__secretcount不可访问,所以会报无此属性的错误.
解决办法如下:
代码如下:
# 1. 可以通过其内部成员方法访问
# 2. 也可以通过访问
classname._classname__attr
#或
classinstance._classname__attr
#来访问,比如:
print count1._justcounter__secretcount
print justcounter._justcounter__secretcount
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> print x
traceback (most recent call last):
file “”, line 1, in
nameerror: name ‘x’ is not defined
>>> x = 1
>>> print x
1
【错误分析】python不允许使用未赋值变量
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> t = (1,2)
>>> t.append(3)
traceback (most recent call last):
file “”, line 1, in
attributeerror: ‘tuple’ object has no attribute ‘append’
>>> t.remove(2)
traceback (most recent call last):
file “”, line 1, in
attributeerror: ‘tuple’ object has no attribute ‘remove’
>>> t.pop()
traceback (most recent call last):
file “”, line 1, in
attributeerror: ‘tuple’ object has no attribute ‘pop’
【错误分析】属性错误,归根到底在于元祖是不可变类型,所以没有这几种方法.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> t = ()
>>> t[0]
traceback (most recent call last):
file “”, line 1, in
indexerror: tuple index out of range
>>> l = []
>>> l[0]
traceback (most recent call last):
file “”, line 1, in
indexerror: list index out of range
【错误分析】空元祖和空列表,没有索引为0的项
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> if x>y:
… x,y = 3,4
… print x,y
file “”, line 3
print x,y
^
indentationerror: unexpected indent
>>> t = (1,2,3,4)
file “”, line 1
t = (1,2,3,4)
^
indentationerror: unexpected indent
【错误分析】一般出在代码缩进的问题
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> f = file(‘1.txt’)
>>> f.readline()
‘aaaaa\n’
>>> f.readline()
‘bbbbb\n’
>>> f.next()
‘ccccc\n’
【错误分析】如果文件里面没有行了会报这种异常
代码如下:
>>> f.next() #
traceback (most recent call last):
file “”, line 1, in
stopiteration
有可迭代的对象的next方法,会前进到下一个结果,而在一系列结果的末尾时,会引发stopiteration的异常.
next()方法属于python的魔法方法,这种方法的效果就是:逐行读取文本文件的最佳方式就是根本不要去读取。
取而代之的用for循环去遍历文件,自动调用next()去调用每一行,且不会报错
代码如下:
for line in open(‘test.txt’,’r’):
print line
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> string = ‘spam’
>>> a,b,c = string
traceback (most recent call last):
file “”, line 1, in
valueerror: too many values to unpack
【错误分析】接受的变量少了,应该是
代码如下:
>>> a,b,c,d = string
>>> a,d
(‘s’, ‘m’)
#除非用切片的方式
>>> a,b,c = string[0],string[1],string[2:]
>>> a,b,c
(‘s’, ‘p’, ‘am’)
或者
>>> a,b,c = list(string[:2]) + [string[2:]]
>>> a,b,c
(‘s’, ‘p’, ‘am’)
或者
>>> (a,b),c = string[:2],string[2:]
>>> a,b,c
(‘s’, ‘p’, ‘am’)
或者
>>> ((a,b),c) = (‘sp’,’am’)
>>> a,b,c
(‘s’, ‘p’, ‘am’)
简单点就是:
>>> a,b = string[:2]
>>> c = string[2:]
>>> a,b,c
(‘s’, ‘p’, ‘am’)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> mydic={‘a’:1,’b’:2}
>>> mydic[‘a’]
1
>>> mydic[‘c’]
traceback (most recent call last):
file “”, line 1, in ?
keyerror: ‘c’
【错误分析】当映射到字典中的键不存在时候,就会触发此类异常, 或者可以,这样测试
代码如下:
>>> ‘a’ in mydic.keys()
true
>>> ‘c’ in mydic.keys() #用in做成员归属测试
false
>>> d.get(‘c’,'”c” is not exist!’) #用get或获取键,如不存在,会打印后面给出的错误信息
‘”c” is not exist!’
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
file “study.py”, line 3
return none
^
dentationerror: unexpected indent
【错误分析】一般是代码缩进问题,tab键或空格键不一致导致
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>>def a():
return a()
>>>a() #无限循环,等消耗掉所有内存资源后,报最大递归深度的错误
file “”, line 2, in a return a()runtimeerror: maximum recursion depth exceeded
class bird:
def __init__(self):
self.hungry = true
def eat(self):
if self.hungry:
print “ahaha…”
self.hungry = false
else:
print “no, thanks!”
该类定义鸟的基本功能吃,吃饱了就不再吃
输出结果:
代码如下:
>>> b = bird()
>>> b.eat()
ahaha…
>>> b.eat()
no, thanks!
下面一个子类singbird,
代码如下:
class singbird(bird):
def __init__(self):
self.sound = ‘squawk’
def sing(self):
print self.sound
输出结果:
代码如下:
>>> s = singbird()
>>> s.sing()
squawk
singbird是bird的子类,但如果调用bird类的eat()方法时,
代码如下:
>>> s.eat()
traceback (most recent call last):
file “”, line 1, in
s.eat()
file “d:\learn\python\person.py”, line 42, in eat
if self.hungry:
attributeerror: singbird instance has no attribute ‘hungry’
【错误分析】代码错误很清晰,singbird中初始化代码被重写,但没有任何初始化hungry的代码
代码如下:
class singbird(bird):
def __init__(self):
self.sound = ‘squawk’
self.hungry = ture #加这么一句
def sing(self):
print self.sound
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
class bird:
def __init__(self):
self.hungry = true
def eat(self):
if self.hungry:
print “ahaha…”
self.hungry = false
else:
print “no, thanks!”
class singbird(bird):
def __init__(self):
super(singbird,self).__init__()
self.sound = ‘squawk’
def sing(self):
print self.sound
>>> sb = singbird()
traceback (most recent call last):
file “”, line 1, in
sb = singbird()
file “d:\learn\python\person.py”, line 51, in __init__
super(singbird,self).__init__()
typeerror: must be type, not classobj
【错误分析】在模块首行里面加上__metaclass__=type,具体还没搞清楚为什么要加
代码如下:
__metaclass__=type
class bird:
def __init__(self):
self.hungry = true
def eat(self):
if self.hungry:
print “ahaha…”
self.hungry = false
else:
print “no, thanks!”
class singbird(bird):
def __init__(self):
super(singbird,self).__init__()
self.sound = ‘squawk’
def sing(self):
print self.sound
>>> s = singbird()
>>> s.
syntaxerror: invalid syntax
>>> s.
syntaxerror: invalid syntax
>>> s.eat()
ahaha…
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> t
(1, 2, 3, 4)
>>> t[0] = 22
traceback (most recent call last):
file “”, line 1, in
t[0] = 22
typeerror: ‘tuple’ object does not support item assignment
【错误分析】元祖不可变,所以不可以更改;可以用切片或合并的方式达到目的.
代码如下:
>>> t = (1,2,3,4)
>>> (22,) + t[1:]
(22, 2, 3, 4)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码如下:
>>> x = 1;
>>> y = 2;
>>> x + = y
file “”, line 1
x + = y
^
syntaxerror: invalid syntax
【错误分析】增强行赋值不能分开来写,必须连着写比如说 +=, *=
代码如下:
>>> x += y
>>> x;y
3
2