学习笔记、永久储存:泡菜、easygui、对象的理解、私有变量、继承

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值