Python常见的错误汇总

时间:2022-02-04 17:53:04

Python常见的错误汇总

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

TypeError: 'NoneType' object is not callable

【错误分析】我是在别的文件中写了一个函数,然后在python console调用使用该函数,出现了上述错误。原因是因为没有对别的文件中函数进行编译,只要打开该调用函数的文件,然后点击运行,之后再在python console中调用使用该函数就不会报错。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

Python常见的错误汇总

【错误分析】第二个参数必须为类,否则会报TypeError,所以正确的应该是这样的:

Python常见的错误汇总

但如果第二个参数是类型对象,则不会报上面的错误,是允许的,比如说:

Python常见的错误汇总

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

Python常见的错误汇总

【错误分析】这个涉及到调用顺序问题,即解析方法的MRO调用顺序,在Python2.7版本之后,这样调用会报错,

必须是子类先放前面,然后才是父类.如下所示,方不会报错.

Python常见的错误汇总

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

Python常见的错误汇总

【错误分析】foo()未带参数self,也未带cls参数,属于类的静态方法,类的静态方法调用,实例不能直接调用,需要再声明一个静态方法

Python常见的错误汇总

或者通过@staticmethod来调用

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

Python常见的错误汇总

【错误分析】__dict__是实例的特殊属性,但在内建属性中,不存在__dict__属性,一般的情况是:

Python常见的错误汇总

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

Python常见的错误汇总

【错误分析】如果定义了构造器,它不应当返回任何对象,因为实例对象是自动在实例化调用后返回的。相应地,__init__()就不应当返回任何对象(应当为None);否则就可能出现冲突,因为只能返回实例。试着返回非None的任何其他对象都会导致TypeError异常

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

1 >>> def f(x, y):
2 print x, y
3 >>> t = ('a', 'b')
4 >>> f(t)
5
6 Traceback (most recent call last):
7 File "<pyshell#65>", line 1, in <module>
8 f(t)
9 TypeError: f() takes exactly 2 arguments (1 given)

【错误分析】不要误以为元组里有两个参数,将元组传进去就可以了,实际上元祖作为一个整体只是一个参数,

实际需要两个参数,所以报错。必需再传一个参数方可.

 >>> f(t, 'var2')
('a', 'b') var2

更常用的用法: 在前面加*,代表引用元组

 >>> f(*t)
'a', 'b'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

1 >>> def func(y=2, x):
2 return x + y
3 SyntaxError: non-default argument follows default argument

【错误分析】在C++,Python中默认参数从左往右防止,而不是相反。这可能跟参数进栈顺序有关。

1 >>> def func(x, y=2):
2 return x + y
3 >>> func(1)
4 3

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

1 >>> D1 = {'x':1, 'y':2}
2 >>> D1['x']
3 1
4 >>> D1['z']
5
6 Traceback (most recent call last):
7 File "<pyshell#185>", line 1, in <module>
8 D1['z']
9 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 "<pyshell#22>", line 1, in <module>
sqrt(4)
TypeError: 'int' object is not callable

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

 >>> seq = [1, 2, 3, 4]
>>> sep = '+'
>>> sep.join(seq) Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
sep.join(seq)
TypeError: sequence item 0: expected string, int found

【错误分析】join是split的逆方法,是非常重要的字符串方法,但不能用来连接整数型列表,所以需要改成:

1 >>> seq = ['', '', '', '']
2 >>> sep = '+'
3 >>> sep.join(seq)
4 '1+2+3+4'

错误:

1 >>> print r'C:\Program Files\foo\bar\'
2 SyntaxError: EOL while scanning string literal

【错误分析】Python中原始字符串以r开头,里面可以放置任意原始字符,包括\,包含在字符中的\不做转义。

但是,不能放在末尾!也就是说,最后一个字符不能是\,如果真 需要的话,可以这样写:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

代码:

 bad = 'bad'  

 try:
raise bad
except bad:
print 'Got Bad!'

错误:

【错误分析】因所用的Python版本2.7,比较高的版本,raise触发的异常,只能是自定义类异常,而不能是字符串。所以会报错,字符串改为自定义类,就可以了。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 错误:

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

执行上面一段代码,错误如下:

【错误分析】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期望得到的是对象,所以该模块必须成功导入。在没导入模块前,不能重载.

 >>> import sys
>>> reload(sys)
<module 'sys' (built-in)>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 错误

 >>> def f(x, y, z):
return x + y + z >>> args = (1,2,3)
>>> print f(args) Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print f(args)
TypeError: f() takes exactly 3 arguments (1 given)

*args,才是将元组中的每个元素作为参数

 >>> f(*args)
6

 错误:

【错误分析】错误原因**匹配并收集在字典中所有包含位置的参数,但传递进去的却是个元祖。

所以修改传递参数如下:

 >>> args = {'a':1,'b':2,'c':3}
>>> args['d'] = 4
>>> f(**args)
1 2 3 4

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Python常见的错误汇总

【错误分析】在函数hider()内使用了内置变量open,但根据Python作用域规则LEGB的优先级:

先是查找本地变量==》模块内的其他函数==》全局变量==》内置变量,查到了即停止查找。

所以open在这里只是个字符串,不能作为打开文件来使用,所以报错,更改变量名即可。

可以导入__builtin__模块看到所有内置变量:异常错误、和内置方法

  import __builtin__
>>> dir(__builtin__)
['ArithmeticError', 'AssertionError', 'AttributeError',...
........................................zip,filter,map]

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 错误

【错误分析】(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()判断某个对象是否可哈希

但列表中元素是可变对象,所以是不可哈希的,所以会报上面的错误.

如果要用列表作为字典中的键,最简单的办法是:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

【错误分析】列表属于可变对象,其append(),sort(),reverse()会在原处修改对象,不会有返回值,

或者说返回值为空,所以要实现反转并排序,不能并行操作,要分开来写

或者用下面的方法实现:

 In [103]: sorted(reversed([2,1,4,3]))
Out[103]: [1, 2, 3, 4]

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 错误:

【错误分析】class是Python保留字,Python保留字不能做变量名,可以用Class,或klass
同样,保留字不能作为模块名来导入,比如说,有个and.py,但不能将其作为模块导入

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

【错误分析】\n默认为换行,\t默认为TAB键.

所以在D:\目录下找不到ew目录下的ext.data文件,将其改为raw方式输入即可。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

【错误分析】错误原因,else, finally执行位置;正确的程序应该如下:

 try:
print 1 / 0 except ZeroDivisionError:
print 'integer division or modulo by zero' else:
print 'Continue Handle other part' finally:
print 'Done'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误:

【错误分析】错误原因,列表解析中,x,y必须以数组的方式列出(x,y)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

报错如下:

 >>>
secretCount is: 1
secretCount is: 2 Traceback (most recent call last):
File "D:\Learn\Python\Learn.py", line 13, in <module>
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

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【错误分析】属性错误,归根到底在于元组是不可变类型,所以没有这几种方法.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【错误分析】空元组和空列表,没有索引为0的项
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【错误分析】一般出在代码缩进的问题
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【错误分析】如果文件里面没有行了会报这种异常

有可迭代的对象的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 "<stdin>", line 1, in <module>
ValueError: too many values to unpack

【错误分析】接受的变量少了,应该是

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【错误分析】当映射到字典中的键不存在时候,就会触发此类异常, 或者可以,这样测试

 >>> 'a' in mydic.keys()
True
>>> 'c' in mydic.keys() #用in做成员归属测试
False
>>> D.get('c','"c" is not exist!') #用get或获取键,如不存在,会打印后面给出的错误信息
'"c" is not exist!'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【错误分析】一般是代码缩进问题,TAB键或空格键不一致导致

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 >>>def A():
return A()
>>>A() #无限循环,等消耗掉所有内存资源后,报最大递归深度的错误
File "<pyshell#2>", 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 "<pyshell#5>", line 1, in <module>
s.eat()
File "D:\Learn\Python\Person.py", line 42, in eat
if self.hungry:
AttributeError: SingBird instance has no attribute 'hungry'

【错误分析】代码错误很清晰,SingBird中初始化代码被重写,但没有任何初始化hungry的代码

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 __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...

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【错误】

1 >>> T
2 (1, 2, 3, 4)
3 >>> T[0] = 22
4 Traceback (most recent call last):
5 File "<pyshell#129>", line 1, in <module>
6 T[0] = 22
7 TypeError: 'tuple' object does not support item assignment

【错误分析】元组不可变,所以不可以更改;可以用切片或合并的方式达到目的.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 >>> X = 1;
>>> Y = 2;
>>> X + = Y
File "<stdin>", line 1
X + = Y
^
SyntaxError: invalid syntax

【错误分析】增强行赋值不能分开来写,必须连着写比如说 +=, *=

 >>> X += Y
>>> X;Y
3
2

参考:http://blog.csdn.net/jerry_1126/article/details/39395899