>>> [(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
人气教程排行