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
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)
>>> 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
>>> 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]
>>> [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
Traceback (most recent call last):
File "D:\Learn\Python\Learn.py", line 13, in
count1.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'
>>> 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
>>> 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!'
>>> 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'