一、traceback模块
错误日志可能用到
import tracebackwhile 1: # 捕获异常的顺序按照先父类后子类try:n1 = int(input('请输入一个被除数'))n2 = int(input('输人除数'))result = n1 / n2except:traceback.print_exc()else:print(result)print('程序结束')
二、pycharm代码调试
打断点 再要打断点代码的前方点击出现红点
点击小虫子开始调试(快捷键shift+F9)
下方原控制台位置点击第二给向下的箭头(快捷键F7),一步步调试,代码上蓝条表示即将运行的代码,下方可以看到数据的变化过程
三、面向对象编程
解决复杂问题,通过面向对象方式便于我们从宏观上把握事情之间复杂的关系,方便我们分析整个系统;微观到具体操作,仍需面向过程处理。
面向对象的三大特征:
封装:提高程序的安全性,将属性方法包装到类对象中。在方法内部属性对属性进行操作,在类对象外部调用方法,无需担心方法内部具体实现,从而隔离了复杂度。
如果属性不希望在类外部被访问前面使用两个"_"。
继承:提高代码的复用性
多态:提高代码可扩展性和可维护性
一、类
# 类的创建class Student: # Student为类名,多个单词也要首字母大写native_place: '河南' # 写在类中的变量,称为类属性def __init__(self, name, age):self.name = name # self.name称为实例属性,将局部变量name的值赋值给实例属性self.age = agedef eat(self): # 在类外定义称为函数,在类内定义称为方法print('学生开吃!')@staticmethoddef method(): # 不写selfprint('我是静态方法!')@classmethoddef cm(cls):print('我是类方法!')
# 对象创建stu = Student('张三', 25) # 根据初始化函数传参print(type(stu))print(stu.name)Student.eat(stu) # 调用的另一种方式stu.eat()print(Student.native_place) # 调用类属性Student.native_place = '北京' # 修改类属性print(stu.native_place)stu2 = Student('李四', 30)print(stu2.native_place)Student.cm() # 调用类方法Student.method() # 调用静态方法# 动态绑定属性的方法stu.gender = '男'print(stu.gender)# 动态绑定方法def attack(self):print(self.name + '发起攻击')stu.attack = attack(stu)stu.attack
二、封装,继承
封装:
class Car:def __init__(self, brand, price):self.brand = brandself.__price = price # 该属性无法再类外被使用def start(self):print('汽车已启动!')def showprice(self):print(self.__price)car = Car('玩具车', 200)car.start() # 在类的外面用到了类里的方法属性(封装)print(car.brand)# print(car.__price)#报错car.showprice() # 在类里面可以被访问print(car._Car__price) # 在类外访问,不建议使用!!!!!
继承:
class Person(object):def __init__(self, name, age):self.name = nameself.age = agedef info(self):print('姓名:' + self.name + ' 年龄:' + str(self.age))class Student(Person): # 继承person类def __init__(self, name, age, stu_no):super().__init__(name, age) # super访问父类方法初始化self.stu_no = stu_nodef info(self): # 方法重写super().info() # 调用父类方法print(self.stu_no)class Teacher(Person): # 继承person类def __init__(self, name, age, teachofyear):super().__init__(name, age) # super访问父类方法初始化self.teachofyear = teachofyearclass StudentTeacher(Teacher, Student): # 多继承,继承两个类passstu = Student('GYQ', 22, '1112132456')stu.info() # 调用父类中的方法
object类,方法重写
是所有类的父类,所有类都有其属性方法
class Student:def __init__(self, name, age):self.name = nameself.age = agedef __str__(self): # 重写__str__方法return '你的名字是:{0},今年{1}岁了'.format(self.name, self.age)stu = Student('ggyyqq', 55)print(dir(stu)) # dir函数查看对象所有属性值print(stu) # 自动调用__str__方法
三、特殊方法和特殊属性
class A:passclass B:passclass C(A, B):def __init__(self, name, age): # 对对象属性初始化self.name = nameself.age = agedef __add__(self, other):return self.name + other.namedef __len__(self):return len(self.name)x = C('sss', 20)y = C('AAA', 22)print(x.__dict__) # 实例对象的属性字典print(C.__dict__) # 类对象的属性字典print(x.__class__) # 实例对象所属的类print(C.__bases__) # 类的父类组成的元组print(C.__base__) # 写在起那面的那个print(C.__mro__) # 类的层次结构print(A.__subclasses__()) # 子类print(x + y) # 加时调用__add__方法print(len(x)) # 方法调用__len__方法
创建对象的过程
四、类的浅拷贝和深拷贝
class CPU:passclass Disk:passclass Computer:def __init__(self, cpu, disk):self.cpu = cpuself.disk = disk# 变量的赋值cpu1 = CPU()cpu2 = cpu1print(cpu1)print(cpu2) # 两者id相同赋值实际是让cpu2指向cpu1指向的对象# 类的浅拷贝:对象包含的子对象内容不拷贝如disk,源对象和拷贝对象引用同一个子对象disk = Disk()computer = Computer(cpu1, disk)import copycomputer2 = copy.copy(computer)print(computer, computer.cpu, computer.disk)print(computer2, computer2.cpu, computer2.disk) # compter的id不同,disk,cpu相同print('--------')# 类的深拷贝computer3 = copy.deepcopy(computer)print(computer, computer.cpu, computer.disk)print(computer3, computer3.cpu, computer3.disk) # compter的id不同,disk,cpu也不同
四、模块
一个py文件就是一个模块,名字不要和自带标准模块相同,模块包含函数,类,语句,类中包含类属性,实例属性 ,类方法,静态方法
导入模块:import 模块名 as 别名 或 from 模块名 import 函数/变量/类
一、常用模块
import sysimport timeimport mathprint(sys.getsizeof('a'))print(sys.getsizeof(222))print(time.time())print(time.localtime(time.time()))print(math.pi)
二、第三方模块的安装
win+R-> cmd->pip install schedule(模块名)
import timeimport schedule#计划def show():print('来了老弟')schedule.every(3).seconds.do(show)while True:schedule.run_pending()time.sleep(1)
五、文件
一、编码格式
Python解释器用的是Unicode(内存)
.py文件在磁盘上使用的是UTF-8存储外存
文件中改变编码格式:#encoding=gbk
二、文件读写io操作
import osif not os.path.exists('./a'):os.mkdir('./a')fp = open('./a/b', 'a+')fp.write('aaa')fp.close()fp = open('./a/b', 'r')print(fp.readlines())fp.close()
with上下文管理器:自动管理上下文资源·,无论什么原因跳出with块,都能保证正确关闭文件
# encodin=gbkwith open('./a.txt', 'w', encoding='gbk') as fp:fp.write('我是嫩爹!我是嫩亲爹!')
六、os模块
os.path和os模块用于对文件目录进行操作
import oslist1=os.listdir('../pythonProject')print(list1)print('---')print(os.getcwd()) # 返回当前路径# os.system('calc.exe') # 执行命令# os.system('notepad.exe')# os.startfile('E:\\Game\\LOL(26)\\TCLS\\Client.exe') # 执行可执行文件