Python笔记(Day_3)
永久储存:泡菜
对象转化为二进制模式的过程为:pickling
反之:unpickling
>>> import pickle
>>> my_list = [123 , 3.14 , 'lkx', [ 'another list' ] ]
>>> pickle_file = open ( 'my_list.pk1' , 'wb' )
>>> pickle.dump(my_list,pickle_file) #读取就是pickle.load()
>>> pickle_file.close()
———————————————————————————————————————————————
try语句:
>>> try :
sum = 1 + '1'
f = open('我为什么是一个文件.txt')
print(f.read())
f.close()
except OSError as reason:
print('文件出错啦\n错误的原因是:' + str(reason))
except TypeError as reason:
print('类型出错啦\n错误的原因是:' + str(reason))
输出:
类型出错啦
错误的原因是:unsupported operand type(s) for +: ‘int’ and ‘str’
sum下面的被跳过了
———————————————————————————————————————————————
expect合并:
>>> try :
sum = 1 + '1'
f = open('我为什么是一个文件.txt')
print(f.read())
f.close()
except (OSError,TypeError):
print('出错啦')
———————————————————————————————————————————————
finally语句:
>>> try :
f = open('我为什么是一个文件.txt','w')
print(f.write('我存在了'))
sum = 1 + '1'
except (OSError,TypeError):
print('出错啦')
finally:
f.close()
输出:
4
出错啦
———————————————————————————————————————————————
raise语句:
raise:手动报错
如:raise OSError
———————————————————————————————————————————————
with语句:
>>> try :
with open('data.txt') as f:
for each_line in f:
print(each_line)
except OSError as reason:
print('出错啦:' + str(reason))
出错啦:[Errno 2] No such file or directory: ‘data.txt’
———————————————————————————————————————————————
图形界面:(三种表达)
>>> import easygui
>>> easygui.msgbox('hello')
>>> from easygui import *
>>> msgbox('hello')
>>> import easygui as g
>>> g.msgbox('hello')
———————————————————————————————————————————————
对象=属性+方法
对象的理解:
class Turtle : #Python 中类名约定以大写字母开头
#属性
color = 'green'
weight = 10
leg = 4
shell = True
mouth = '大嘴'
#方法
def climb(self):
print("我正在努力地向前爬。。。")
def run(self):
print("我正在飞快地向前跑。。。")
def bite(self):
print("咬死你")
def eat(self):
perint("有得吃,就够了")
def sleep(self):
print("睡了,晚安。。。")
———————————————————————————————————————————————
>>> Turtle().sleep()
睡了,晚安。。。
>>> tt=Turtle()
>>> tt.sleep()
睡了,晚安。。。
———————————————————————————————————————————————
1):
>>> class Ball:
def setName(self,name):
self.name = name
def kick(self):
print("我叫%s" % self.name)
>>> a=Ball()
>>> a.setName('sfs')
>>> a.kick()
我叫sfs
2):
init:
>>> class Ball:
def __init__(self,name):
self.name = name
def kick(self):
print("我叫%s" % self.name)
>>> b=Ball('sf')
>>> b.kick()
我叫sf
私有变量:
>>> class Person:
__name='lkx'
def getName(self):
return self.__name
>>> p=Person()
>>> p.__name
Traceback (most recent call last):
File "<pyshell#124>", line 1, in <module>
p.__name
AttributeError:=**'Person' object has no attribute '__name'**
>>> p.getName()
'lkx'
>>> p._Person__name
'lkx'
class Parent:
def hello(self):
print(“正在调用父类的方法”)
———————————————————————————————————————————————
继承:
class MyList(list):#继承list的功能
pass
>>> class Child (Parent):
pass
>>> p = Parent()
>>> p.hello()
正在调用父类的方法
>>> c=Child()
>>> c.hello()
正在调用父类的方法
>>> class Child(Parent):
def hello(self):
print("正在调用子类的方法")
>>> c = Child()
>>> c.hello()
正在调用子类的方法 # 覆盖父类
>>> p.hello()
正在调用父类的方法
———————————————————————————————————————————————
鱼的移动:
import random as r
class Fish:
def __init__(self):
self.x = r.randint(0,10)
self.y = r.randint(0,10)
def move(self):
self.x -=1
print("我的位置是:",self.x,self.y)
class Goldfish(Fish):
pass
class Carp(Fish):
pass
class Salmon(Fish):
pass
class Shark(Fish):
def __init__(self):
Fish.__init__(self) #super.__init__()#super指代父类Fish
self.hungry = True
def eat (self):
if self.hungry:
print("吃货的梦想就是天天有的吃")
self.hungry = False
else :
print("太撑了,吃不下了")
———————————————————————————————————————————————
小游戏:
import easygui as g
import sys
while 1:
g.msgbox("hi,welcome to my first game!")
msg = "请问你希望在我这里学到什么知识呢?"
title = "小游戏互动"
choices = ["拍拖","编程","XXOO","琴棋书画"]
choice = g.choicebox(msg,title,choices)
# note that eo convert choices to string , in case
# the user cancelled thr choices , and we got None
g.msgbox("你的选择是:" + str(choice),"结果")
msg = "你希望重新开始吗?"
title = "请选择"
if g.ccbox(msg,title): # show a continue/cancel dialog
pass # user chose continue
else :
sys.exit(0) # user chose cancel