今天主要学习了python 关于类的知识,以及读写文件、存储json和异常捕捉等知识
主要的内容都在以下的代码里,注释比较全,阅读理解很容易
# by Fan 20180709
# 类 class
# 示例 学生类 名字 性别 年龄
class Students:
def __init__(self, name, sex, age):
self.name = name
self.sex = sex
self.age = age
self.hight = 175
def study(self):
print(self.name + " 学习去啦" + '! 他身高' + str(self.hight) + ',喜欢学习!')
def paly_game(self):
print(self.name + ' 玩去啦' + '! 她身高' + str(self.hight) + ',喜欢打篮球!')
def set_hight(self, hight):
if hight >= 150:
self.hight = hight
else:
print("I'm sorry! You hight so di! So height 还是初始化定义的175")
def add_height(self, heights):
self.hight += heights
student = Students('小明', '男', 20)
student.set_hight(140) # 第一种方法:通过方法修改属性的值
student.study()
student.add_height(20)
student.study()
student2 = Students('小丽', '女', 21)
student2.hight = 170 # 第二种方法:直接修改属性的值
student2.paly_game()
class ClassNum:
def __init__(self, num=1):
self.num = num
def show_class_num(self):
print('这是三年级' + str(self.num) + '班')
class ClassMater(Students):
def __init__(self, name, sex, age):
super().__init__(name, sex, age)
self.weight = 60
self.class_num = ClassNum() # 独立类初始化
def do_work(self):
print(self.name + '做家务去啦' + '他体重' + str(self.weight))
def paly_game(self): # 重写父类方法
print(self.name + ' 玩去啦 2222' + '! 她身高' + str(self.hight) + ',喜欢打篮球!')
class_mater = ClassMater('小华', '男', 18)
class_mater.set_hight(151) # 继承父类方法
class_mater.study() # 继承父类方法
class_mater.do_work() # 继承父类方法
class_mater.paly_game() # 重写父类方法
class_mater.class_num.show_class_num() # 独立类 (小类) 调用
# 模块 导入 (为了阅读代码简洁,而不是很长的py文件)
# from 文件名 import 类名
# 在一个模块中,也可以存储多个类
# from 文件名 import 类名,类名,类名
# 也可以导入整个模块,调用 .来调用类名
# from 文件名 使用时 文件名.类名()
# 导入模块中所有的类
# from 文件名 import * (不推荐此种做法,一般不用)
# 在一个模块中也可以导入另一个模块
# python 标准库
# 标准库是一组模块,熟悉后,可以直接调用现成的模块来使用 比如下方
from collections import OrderedDict
# 我没有定义collections 模块,也没有写 OrderedDict 类 ,直接导入,
# 因为已经导入了 库中的这个文件 (库其实也是代码集合(别人写好的),只是封装(写)好之后,直接拿来用的)
# 文件 和 异常
# 打开文件
# 关键字 with 在不再需要访问文件后关闭
with open('生成exe文件.txt') as file_test:
contact = file_test.read()
print(contact)
# 文件系统路径
# os x 或者 linux系统 中
file_path = '/home/ehmatthes/../../..'
# windows 系统中
file_path = 'D:\est\est.txt'
print(file_path)
# 示例
contacts = ''
try:
with open('yamltest.yaml') as file_yaml:
contacts = file_yaml.read().rstrip()
print(contacts)
len_contact = len(contacts)
print(len_contact)
except FileNotFoundError:
print('文件不存在')
# name = input('请输入你的身份:')
# if name in contacts:
# print("你是个有身份的人")
# else:
# print("你是个黑户")
# 写入文件
# 空文件开始 写入多行用\n
file_name = 'test2.txt'
# with open(file_name, 'w') as file_input:
# file_input.write('===========Hello men ========================')
#
# with open(file_name, 'a') as file_input02:
# file_input02.write('我就是看看,不说话')
# with open(file_name) as test_read:
# contacts_01 = test_read.read()
# print(contacts_01)
# 总结 open()函数中 'w' 为重新写入,以前的内容都会清理掉
# 而 'a'的话,则是 附加内容
# 示例 依次输入 逐行排列
# a = 0
# while a < 10:
# name = input('请输入您的名字:')
# print(name + ',下午好!')
# with open(file_name, 'w') as file_store:
# file_store.write(name + '\n')
# if a == 9:
# print('输入结束了,谢谢使用')
# a = a+1
# 异常处理 (捕捉异常,并提示消息)
# 使用异常的优点:出现异常,避免程序崩溃
try:
print(5/0)
except ZeroDivisionError:
print('除数不能为0')
else:
print('赞一个')
# 存储数据
import json
numbers = [1, 2, 3, 4, 5, 6]
file_name_json = 'test.json'
with open(file_name_json, 'w') as f_json:
json.dump(numbers, f_json)
with open(file_name_json) as r_json:
num = json.load(r_json)
print(num)