try-except-else
try: li = [1, 2, 3, 4] # li[4] # 报错 我想把异常信息打印出来 # except IndexError as e: # e = IndexError # print(e) # print("hello") except Exception as e: # 类的对象实例调用__class__属性时会指向该实例对应的类,而后再调用 __name__ 就会输出该实例对应的类的类名 print(e.__class__.__name__,":",e) else: print('123') """ 当try程序不报错时else正常执行 否则不执行 """
123
进程已结束,退出代码0
try-except-finally
try: li = [1, 2, 3, 4] li[4] # 报错 我想把异常信息打印出来 # except IndexError as e: # e = IndexError # print(e) # print("hello") except Exception as e: # 类的对象实例调用__class__属性时会指向该实例对应的类,而后再调用 __name__ 就会输出该实例对应的类的类名 print(e.__class__.__name__,":",e) else: print('123') finally: print('我最NB') """ 不管你报不报错 程序都会执行finally的语句 """
IndexError : list index out of range
我最NB进程已结束,退出代码0
主动触发异常
def test(level): if level<5: raise Exception("没熟没熟") try: test(6) except Exception as e: print(e,'asd') else: print("熟了 请吃")
熟了 请吃
进程已结束,退出代码0
作业
print() """ 创建一个学生类: 属性:姓名,年龄,学号 使用init方法 方法:展示学生信息(自我介绍) 使用str方法 """ class Student: def __init__(self,name,age,id_): self.name = name self.age = age self.id_ = id_ def __str__(self): return f"我叫{self.name},我今年{self.age}岁,我的学号是{self.id_}" zs = Student('张三',18,1) print(zs)
我叫张三,我今年18岁,我的学号是1
进程已结束,退出代码0
单继承
# 单继承 class Grandfather: def __init__(self): print('Grandfather') def sleep(self): print("sleep") def eat(self): print("eat") class Father(Grandfather): def eat(self): print("eatq") def drink(self): print("drink") class Son(Father): def study_python(self): print("python") s = Son() s.study_python() s.eat() s.sleep()
Grandfather
python
eatq
sleep进程已结束,退出代码0
多态与鸭子类型
# class Animal(object): # """动物类""" # def func(self): # print('动物发出了声音') # # # class Cat(Animal): # """猫类""" # def func(self): # print('喵 喵 喵') # # # class Dog(Animal): # """狗类""" # def func(self): # print('汪 汪 汪 ') # # class Hero: # def func(self): # print('这个是英雄类的方法,不是动物类的对象') # # # def work01(Animal): # Animal.func() # 对象名.方法名() # # # work01(Hero()) # 传入的对象 # # 传入不同的对象,产生不同的结果 # # 调用灵活 更容易编写出通用的代码 class Duck: def quack(self): print("嘎嘎嘎嘎。。。。。") class Bird: def quack(self): print("bird imitate duck....") class geese: def quack(self): print("geese imitate duck....") def in_the_forest(asd): asd.quack() duck = Duck() bird = Bird() geese = geese() for x in [duck, bird, geese]: in_the_forest(x)
嘎嘎嘎嘎。。。。。
bird imitate duck....
geese imitate duck....进程已结束,退出代码0
多继承
# print() # """情景1""" # class Father1: # def run(self): # print("father1 run") # # # class Father2: # def run(self): # print("father2 run") # # # class Son(Father2, Father1): # 拥有相同方法时,左边优先执行 # pass # # # s = Son() # s.run() """情景2""" # class Grandfather: # pass # # def sleep(self): # # print("Grandfather sleep") # # # class Father1(Grandfather): # def run(self): # print("father1 run") # # # class Father2: # def sleep(self): # print(" Father2 sleep") # # # class Son(Father1, Father2): # 必须要左边的执行完了,才会执行右边的父类 # pass # # # s = Son() # s.sleep() class Grandfather1(object): # ctrl+B def sleep(self): print("sleep 12") class Father1(Grandfather1): def sleep(self): print("sleep 12") def run(self): print("father1 run") class Father2(Grandfather1): def sleep(self): print("sleep 6") class Son(Father1, Father2): # 如果同根的话,根是最后才执行的 pass s = Son() s.sleep() print(Son.__mro__) # 通过mro方法可以知道程序执行或者继承顺序的情况
sleep 12
(<class '__main__.Son'>, <class '__main__.Father1'>, <class '__main__.Father2'>, <class '__main__.Grandfather1'>, <class 'object'>)进程已结束,退出代码0
封装实例
class Person: def __init__(self, name, age): self.__name = name self.age = age def __getname(self): # print(self.__name) print(f'{self.__name}') def printinfo(self): print(f'{self.__name},{self.age}') p = Person('zhangsan', 18) p.printinfo() # 没有真正意义上的私用 p._Person__getname() # 不建议同学们使用
zhangsan,18
zhangsan进程已结束,退出代码0
异常快速体验
# ipt = input("请输入:") # ipt = float(ipt) # print(ipt) # ipt = 1 # print(ipt) # try: # # 正常程序时执行代码块 # ipt = input("请输入:") # ipt = float(ipt) # print(ipt) # # except Exception as e: # print('异常信息为:',e) # # 异常程序时执行代码块 # ipt = 1 # print(ipt) li = [1, 2, 3, 4] # print(li[4]) # IndexError: list index out of range a = 1 b = 0 # c = a / b # print(c) # ZeroDivisionError: division by zero # print(int("a")) # ValueError: invalid literal for int() with base 10: 'a' try: li = [1, 2, 3, 4] li[4] # 报错 我想把异常信息打印出来 # except IndexError as e: # e = IndexError # print(e) # print("hello") # 需要注意的是,该捕获方式仅能捕获IndexError这一类异常。那么实际上,这种细分的异常种类有很多, # 可以通过其共同父类Exception捕获输出。 except Exception as e: print(e) # 类的对象实例调用__class__属性时会指向该实例对应的类,而后再调用 __name__ 就会输出该实例对应的类的类名 print(e.__class__.__name__,": ",e,sep='')
list index out of range
IndexError: list index out of range进程已结束,退出代码0
断言
# print("*" * 20) # if 2 == 2: # raise AssertionError # print("*" * 20) print("*" * 20) if 2 == 2: try: raise AssertionError except Exception as e: print(e.__class__.__name__) print("*" * 20)
********************
AssertionError
********************进程已结束,退出代码0
方法的重写
# 父类 class A(): def __init__(self): print('A') def test(self): print("20w") # 子类 class B(A): def __init__(self): print('B') # A.__init__(self) # 类名.方法名() # super(B, self).__init__() def test(self): print("100w") super(B, self).test() # 同时实现父类的功能 # A.test(self) b = B() b.test()
B
100w
20w进程已结束,退出代码0
自定义异常
print() """判断密码长度 如果密码长度小于6 主动触发异常 并抛出异常信息(打印提示)""" class ShortInputError(Exception): # 初始化方法 def __init__(self, mypasswordlen, minlenght): self.mypasswordlen = mypasswordlen self.minlenght = minlenght def __str__(self): return f'你输入的密码长度为{self.mypasswordlen},不能低于{self.minlenght}' def fun(): try: password = input('请输入你的密码:') if len(password) < 6: raise ShortInputError(len(password), 6) except Exception as e: print(e) else: print('密码长度符合!!!') for i in range(1,6): fun()
请输入你的密码:111
你输入的密码长度为3,不能低于6
请输入你的密码:10000000000
密码长度符合!!!
请输入你的密码:123556
密码长度符合!!!
请输入你的密码: