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