1、pickle库:腌泡菜
>>> import pickle #导入库
>>> my_list = [1,2,['nihao'],3.14]
>>> pickle_file = open('D://untitled//my_list.pkl','wb')#这里是wb形式!!!!!!
#而且open()里面是路径,不可以直接写文件名字,会报错
>>> pickle.dump(my_list,pickle_file) #吧my_list数据倒进文件pickle_file中去
>>> pickle_file.close() #存完数据要把文件关闭,不然白存了
#以上是怎么存东西
>>> pickle_file = open('D://untitled//my_list.pkl','rb') #这里是rb(二进制)形式!!!!!
>>> my_list2 = pickle.load(pickle_file) #读取文件
>>> print(my_list2)
[1, 2, ['nihao'], 3.14]
#以上是怎么读取
2、异常处理
(1)AssertionError
>>> my_list = ['nihao']
>>> assert len(my_list) > 0
>>> my_list.pop()
'nihao'
>>> assert len(my_list) > 0
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
assert len(my_list) > 0
AssertionError
(2)AttributaError:尝试访问未知不存在的对象属性
>>> my_list.nihao()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
my_list.nihao()
AttributeError: 'list' object has no attribute 'nihao'
(3)IndexError:索引不存在报错
>>> my_list
[]
>>> my_list[3]
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
my_list[3]
IndexError: list index out of range
(4)KeyError
>>> my_dict = {1:'nihao',2:'ni'}
>>> my_dict[1]
'nihao'
>>> my_dict[3]
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
my_dict[3]
KeyError: 3
(5)SyntaxError:语法报错(这个比较熟悉了,不写了)
(6)TypeError
>>> 1+'1'
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
1+'1'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
(7)ZeroDivisionError:除数为零报错
>>> 1/0
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
1/0
ZeroDivisionError: division by zero
3、try-except语句
try:
检测范围
except Exception[as reason]:
出现异常后处理代码
finally:
无论如何都会被执行的代码 #比如保存文件:f.close()
(1)单个异常检测
f = open('C://为啥我这么好看.txt')
print(f.read())
f.close()
报错内容:文件不存在
===================== RESTART: D:/untitled/fileerror.py =====================
Traceback (most recent call last):
File "D:/untitled/fileerror.py", line 1, in <module>
f = open('C://为啥我这么好看.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'C://为啥我这么好看.txt'
加入异常检测:
try:
f = open('C://为啥我这么好看.txt')
print(f.read())
f.close()
except OSError:
print('没这个文件')
结果:
>>>
===================== RESTART: D:/untitled/fileerror.py =====================
没这个文件
>>>
修改一下:
try:
f = open('C://为啥我这么好看.txt')
print(f.read())
f.close()
except OSError as reason:
print('没这个文件,Error reason is: ' + str(reason))
结果是:
>>>
===================== RESTART: D:/untitled/fileerror.py =====================
没这个文件,Error reason is: [Errno 2] No such file or directory: 'C://为啥我这么好看.txt'
>>>
(2)多个异常检测:
若出现的异常没有对应的代码处理,则会显示原来报错的红字;
检测到第一个异常,后面就不会继续检测了。
try:
f = open('C://为啥我这么好看.txt')
print(f.read())
f.close()
sum = 1 +'1'
except OSError as reason:
print('没这个文件,Error reason is: ' + str(reason))
except TypeError as reason:
print('类型问题' + str(reason)
也可以写成这样:
except(OSError or TypeError )
4、else 语句
(1)要么怎样,要么不怎样:if和else
(2)干完了能怎样,干不完就别想怎样:while语句和else
(3)没有问题,就干吧:
try:
int('123')
except ValueError as reason:
print('出错!')
else:
print('一切正常!')
结果:
>>>
===================== RESTART: D:/untitled/fileerror.py =====================
一切正常!
5、with语句:自动帮你保存文件
try:
with open('data.txt','w') as f:
for each_line in f:
print(each_line)
except OSError as reason:
print('出错' + str(reason))
6、图形用户界面:EasyGui
(1)直接用命令行安装
pip install easygui
(2)调用easygui库(推荐方式)
>>> import easygui as g
>>> g.msgbox('nishidapianzi')
'OK'
7、对象 = 属性 + 方法
(1)类名:命名约定以大写字母开头
假如:类名叫Tt(),里面的函数叫ee()
那么:可以直接用下面这一行调用
Tt.ee()
(2)继承
#继承的内容不可以和申明的变量名字相同,不然就会报错
>>> list = [1,2,3,5,6]
>>> list.append(9)
>>> list
[1, 2, 3, 5, 6, 9]
>>> class Mylist(list)
SyntaxError: invalid syntax
>>>
>>> class Mylist(list):
pass
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
class Mylist(list):
TypeError: list expected at most 1 arguments, got 3
>>> del list
改成这样就对了:Mylist继承list这个列表的方法
>>> list1 = [1,2,3]
>>> class Mylist(list):
pass
>>> list2 = Mylist()
>>> list2.append(8)
>>> list2
[8]
(3)多态
同一个名字,但是实现的内容不一样
>>> class A:
def fun(self):
print('这里是A方法')
>>> class B:
def fun(self):
print('这里是B方法')
>>> a = A()
>>> b = B()
>>> a.fun()
这里是A方法
>>> b.fun()
这里是B方法
8、异常处理
参考:https://www.runoob.com/python/python-exceptions.html
try:
<语句> #运行别的代码
except <名字>:
<语句> #如果在try部份引发了'name'异常
except <名字>,<数据>:
<语句> #如果引发了'name'异常,获得附加的数据
else:
<语句> #如果没有异常发生
try的工作原理是,当开始一个try语句后,python就在当前程序的上下文中作标记,这样当异常出现时就可以回到这里,try子句先执行,接下来会发生什么依赖于执行时是否出现异常。
如果当try后的语句执行时发生异常,python就跳回到try并执行第一个匹配该异常的except子句,异常处理完毕,控制流就通过整个try语句(除非在处理异常时又引发新的异常)。
如果在try后的语句里发生了异常,却没有匹配的except子句,异常将被递交到上层的try,或者到程序的最上层(这样将结束程序,并打印缺省的出错信息)。
如果在try子句执行时没有发生异常,python将执行else语句后的语句(如果有else的话),然后控制流通过整个try语句。
9、什么是组合???????``
// 乌龟类
class Turtle:
def __init__(self, x):
self.num = x
// 鱼类
class Fish:
def __init__(self, x):
self.num = x
// 水池类
class Pool:
def __init__(self, x, y):
self.turtle = Turtle(x) // 组合乌龟类进来
self.fish = Fish(y) // 组合鱼类进来
def print_num(self):
print("水池里总共有乌龟 %d 只,小鱼 %d 条!" % (self.turtle.num, self.fish.num))
>>> pool = Pool(1, 10)
>>> pool.print_num()