Python常见异常类型
ZeroDivisionError:除或者取模为零异常
IndexError:序列中没有此索引异常
KeyError:映射中没有这个键
NameError:未声明初始化对象(没有属性)
SyntaxError:Python语法错误
ValueError:传入无效的参数
traceback模块的使用
import traceback
try:
print('__________________________')
num=10/0
except BaseException as e:
traceback.print_exc()
打印结果
Traceback (most recent call last):
File "F:/AIot/Python01/main.py", line 5, in <module>
num=10/0
ZeroDivisionError: division by zero
__________________________
编程思想
类
类是多个类似事物组成的群体的统称。能够帮助快速理解判断事物的性质。
定义类
class Student: #Student为类的名称(类名)由一个或多个单词组成每个单词的首字母大写,其余小写
pass
print(id(Student)) #开辟空间
print(type(Student)) #<class 'type'>
print(Student)
class Student: #Student为类的名称(类名)由一个或多个单词组成每个单词的首字母大写,其余小写
native_pace='吉林' #直接写在类里的变量,称为类属性
def __init__(self,name,age):
self.name=name #self.name称为实体属性,进行了一个赋值操作,将局部变量的name的值赋给实体属性
self.age=age
#实例方法
def eat(self):
print('学生在吃饭。。。。')
#静态方法
@staticmethod
def method():
print('我使用了staticmethod进行修饰,所以我是静态方法')
#类方法
@classmethod
def cm(cls):
print('我是类方法,因为我使用了classmethod进行修饰')
#在类之外定义的称为函数,在类之内定义的称为方法
def drink():
print('喝水')
创建对象
#创建Student类的对象
stu1=Student('张三',20)
print(id(stu1))
print(type(stu1))
print(stu1)
print('____________________')
print(id(Student)) #Student是类的名称
print(type(Student))
print(Student)
类属性、类方法、静态方法
#类属性的使用方式
print(Student.native_pace)
stu1=Student('张三',20)
stu1=Student('李四',30)
print(stu1.native_pace)
print(stu1.native_pace)
Student.native_pace='天津'
print(stu1.native_pace)
print(stu1.native_pace)
类方法的调用
Student.cm()
结果
我是类方法,因为我使用了classmethod进行修饰
静态方法:
Student.method() #静态方法直接使用类名去使用
结果
我使用了staticmethod进行修饰,所以我是静态方法
动态绑定属性和方法
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print(self.name+'在吃饭')
stu1=Student('张三',10)
stu2=Student('李四',20)
print(id(stu1))
print(id(stu2))
print('________________________')
stu2.gender='女'
print(stu1.name,stu1.age)
print(stu2.name,stu2.age,stu2.gender)
print('________________________')
stu1.eat()
stu2.eat()
def show():
print('定义在类之外的,称为函数')
stu1.show=show
stu1.show()
面向对象的三大特征
封装:提高安全性
继承:提高复用性
多态:提高扩展性和可维护性
class Car:
def __init__(self,brand):
self.brand=brand
def start(self):
print('汽车已启动')
car=Car('宝马X5')
car.start()
print(car.brand)
class Student:
def __init__(self,name,age):
self.name = name
self.__age=age
def show(self):
print(self.name,self.__age)
stu=Student('张三',20)
stu.show()
print(stu.name)
print(dir(stu))
print(stu._Student__age) #在类的外部可以通过_Student__age进行访问
继承:
如果一个类没有继承任何类,则默认继承object
Python支持多继承,定义子类时,必须在其构造函数中调用父类的构造函数
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person):
def __init__(self,name,age,stu_no):
super().__init__(name,age)
self.stu_no=stu_no
class Teacher(Person):
def __init__(self,name,age,teachofyear):
super().__init__(name,age)
self.teachofyear=teachofyear
stu=Student('张三',20,'1992')
teacher=Teacher('李四',30,10)
stu.info()
teacher.info()
重写
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person):
def __init__(self,name,age,stu_no):
super().__init__(name,age)
self.stu_no=stu_no
def info(self):
super().info()
print(self.stu_no)
class Teacher(Person):
def __init__(self,name,age,teachofyear):
super().__init__(name,age)
self.teachofyear=teachofyear
def info(self):
super().info()
print(self.teachofyear)
stu=Student('张三',20,'1992')
teacher=Teacher('李四',30,10)
stu.info()
print('_____________________')
teacher.info()
object类
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return '我的名字是{0},今年{1}岁'.format(self.name,self.age)
stu=Student('张三',20)
print(stu)
1、内置dir()可以查看指定对象所有属性
2、str()方法,用于返回一个对于“对象的描述”,对应于内置函数str()经常用于print()方法,帮助查看对象的信息,所以经常会对__str__()重写
多态的实现
class Animal(object):
def eat(self):
print('动物会吃')
class Dog(Animal):
def eat(self):
print('狗吃骨头')
class Cat(Animal):
print('猫吃骨头')
class Person:
def eat(self):
print('人吃五谷杂粮')
def fun(obj):
obj.eat()
fun(Cat())
fun(Dog())
fun(Animal())
print('______________')
fun(Person())
特殊的属性和方法
a=10
b=20
c=a+b
d=a.__add__(b)
print(c)
print(d)
class Student:
def __init__(self,name):
self.name=name
def __add__(self, other):
return self.name+other.name
stu1=Student('张三')
stu2=Student('李四')
s=stu1+stu2 #实现了两个对象的加法运算
print(s)
s1=stu1.__add__(stu2)
print(s1)
类的赋值
变量的赋值操作:
只是形成两个变量,实际上还是指向同一个对象
class CPU:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
cpu1=CPU()
cpu2=cpu1
print(cpu1,id(cpu1))
print(cpu2,id(cpu2))
浅拷贝:
Python拷贝一般都是浅拷贝,拷贝时,对象包含的子对象内容不拷贝,因此,源对象与拷贝对象会引用同一个子对象
#类的浅拷贝
disk = Disk() #创建一个硬盘类的对象
computer=Computer(cpu1,disk) #创建一个计算机类的对象
import copy
computer2=copy.copy(computer)
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
深拷贝:
使用copy模块的deepcopy函数,递归拷贝对象中包含的子对象、源对象和拷贝对象所有的子对象也不相同
#深拷贝
computer3 = copy.deepcopy(computer)
print(computer,computer.cpu,computer.disk)
print(computer3,computer3.cpu,computer3.disk)
模块
1、一个模块可以包含多个函数、类、语句
2、在Python中一个扩展名为.py的文件就是一个模块
3、使用模块的好处:方便其他程序和脚本的导入并使用;避免函数名和变量名冲突;提高代码的可维护性;提高代码的可重用性
模块化编程的好处
创建模块:名称尽量不要与Python自带的标准模块名称相同
导入模块:
import math
print(id(math))
print(type(math))
print(math)
print(math.pi)
print('____________________')
print(dir(math))
def add(a,b):
return a+b
if __name__=='__main__':
print(add(10,20))
import calc2
print(calc2.add(100,200))
Python中的包
包是一个分层次的目录结构,作用:代码规范、避免模块名称冲突
import 包名.模块名
使用import方式进行导入时,只能跟包名或者模块名
import pageage1.module_A as a
print(a.a)
第三方模块
第三方模块的安装
pip install 模块名
第三方模块的使用
import 模块名
编码格式
文件读写原理
先进先出
file = open('a.txt', 'r', encoding='utf-8')
print(file.readlines())
file.close()
文件的打开模式
file = open('b.txt', 'w', encoding='utf-8')
file.write('helloworld')
file.close()
会创建一个b.txt文件
文件对象的常用方法
file = open('b.txt', 'a', encoding='utf-8')
lst=['java','go','python']
file.writelines(lst)
file.close()
with语句
with open('a.txt','r',encoding='utf-8') as file:
print(file.read())
目录操作
os模块与操作系统相关的一个模块
import os
os.system('notepad.exe')
os.system('calc.exe')
os模块与os.path模块用于对目录或者文件进行操作
os.path的常用方法
学生信息管理系统