Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 425|回复: 0

python异常(高级) Exception

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-7-18 13:14:45 | 显示全部楼层 |阅读模式

    异常(高级) Exception

      异常回顾:
        try-except 语句 捕获(接收)异常通知,把异常流程变为正常流程
        try-finally 语句 执行必须要执行的语句.
        raise 语句 发送异常通知,同时进入异常流程
        assert 语句 发送AssertionError异常
        with 语句

    with语句
      语法:
        with 表达式1 [as 变量1], 表达式2 [as 变量2], ...:
          语句块
      作用:
      使用于对资源进行访问的场合,确保使用过程中不管是否发生异常都会执行必要的清理操作,并释放资源
      如: 文件使用后自动关闭,线程中锁的自动获取和释放等

      try:
      # file = open("../day19.txt")
      with open('../day19.txt') as file:
        line1 = file.readline()
        print("第一行内容是:", line1)
        n = int(line1) # with语句保证在出异时,文件也能被关闭
        print(n)
    
      except OSError:
        print("文件打开失败")
      except ValueError:
        print('读写文件时出错')
    View Code

    说明:
      with语句同try-finally语句一样,不会改变程序的状态(异常或正常状态) 

    环境管理器:
      类内有'__enter__' 和 '__exit__' 实例方法的类被称为环境管理器能够用with语句进行管理的对象必须是环境管理器
      __enter__将在进入with语句之前被调用,并返回由as 变量管理的对象
      __exit__ 将在离开with语句时被调用,且可以用参数来判断在离开with语句时是否有异常发生并做出相应的处理

      class A:
        def __enter__(self):
          print("__enter__方法被调用")
          # 此处打开文件
          return self # self 将被with as后的变量绑定
    
        def __exit__(self, exc_type, exc_val, exc_tb):
          print("__exit__方法被调用")
          # 此处关闭文件
          if exc_type is None:
            print("正常离开with语句")
          else:
            print("异常离开with语句")
            print(exc_type, exc_val, exc_tb)
    
      try:
        with A() as a:
          print("这是with内的语句")
          err = ValueError("故意抛出一个错误")
          raise err
    
      except ValueError:
        print("with语句内出现异常!!")
    View Code

    异常类:
      BaseExcetion 类是一切异常类的基类
      自定义的异常类型必须直接或间接的继承自BaseExcetion类

    运算符重载
      让自定义的类生成的对象(实例) 能够使用运算符进行操作

      作用:
        让自定义类的实例像内建对象一样进行运算符操作
        让程序简洁易读
        对自定义对象将运算符赋予新的运算规则

      说明:
        运算符已经有固定的含义,不建议改变原有运算符的含义
      方法名          运算符和表达式    说明
      __add__(self, rhs)    self + rhs      加法
      __sub__(self, rhs)    self - rhs      减法
      __mul__(self, rhs)    self * rhs      乘法
      __truediv__(self, rhs)  self / rhs      除法
      __floordiv__(self, rhs)  self // rhs      地板除法
      __mod__(self, rhs)    self % rhs      求余
      __pow__(self, rhs)    self ** rhs      幂运算

      rhs (right hand side)   右手边

    二元运算符的重载方法:
      def __xxx__(self, other):
        ...

      class MyNumber:
        def __init__(self, value):
          self.data = value
    
        def __repr__(self):
          return "MyNumber(%d)" % self.data
    
        def __add__(self, other):
          temp = self.data + other.data
          obj = MyNumber(temp) # 创建一个新的对象
          return obj
        def __sub__(self, other):
          temp = self.data - other.data
          obj = MyNumber(temp) # 创建一个新的对象
          return obj
    
      n1 = MyNumber(100)
      n2 = MyNumber(200)
    
      # n3 = n1.__add__(n2)
    
      n3 = n1 + n2 # 等同于 n3 = n1.__add__(n2)
      print(n1, "+", n2, '=', n3)
      n4 = n1 - n2
      print(n1, "-", n2, '=', n4)
    View Code

    反向算术运算符的重载
      当运算符的左侧为内建类型时,右侧为自定义类的对象进行算术运算符运算时,会出现TypeError错误,因无法修改内建类型的代码来实现运算符重载,此时需要反向算术运算符重载

    方法如下:
      方法名           运算符和表达式    说明
      __radd__(self, lhs)    lhs + self      加法
      __rsub__(self, lhs)    lhs + self      减法
      __rmul__(self, lhs)    lhs * self      乘法
      __rtruediv__(self, lhs)  lhs / self      除法
      __rfloordiv__(self, lhs)  lhs // self      地板除法
      __rmod__(self, lhs)    lhs % self      求余
      __rpow__(self, lhs)    lhs ** self      幂运算

      lhs (left hand side)    左手边

      class MyList:
        def __init__(self, iterable=()):
          self.data = [x for x in iterable]
    
        def __add__(self, rhs):
          L = self.data + rhs.data
          return MyList(L)
    
        def __repr__(self):
          return "MyList(%s)" % self.data
    
        def __mul__(self, rhs):
          L = self.data * rhs
          return MyList(L)
    
        def __rmul__(self, lhs):
          print("__rmul__被调用")
          return MyList(self.data * lhs)
    
      L1 = MyList(range(1, 4))
      L2 = MyList([4, 5, 6])
    
      L5 = L1 * 2 # L5 = L1.__mul__(2)
      print(L5) # MyList([1, 2, 3, 1, 2, 3])
    
      L6 = 2 * L1 # L1.__rmul__(2) 2.__mul__(L1)
      print(L6) # ???
    View Code

    复合赋值算术运算符的重载
      以复合赋值算述运算符 x += y 主为例,此运算符会优先调用x.__iadd__(y) ,如果没有__iadd__方法时,会将复合赋值运算符拆解为 x = x + y然后调用x = x.__add__(y)方法,如再不存在__add__方法,则会触发TypeError错误
      其它复合赋值运算符有相同的规则

      方法名           运算符和表达式   说明
      __iadd__(self, rhs)    self += rhs    加法
      __isub__(self, rhs)    self -= rhs    减法
      __imul__(self, rhs)    self *= rhs    乘法
      __itruediv__(self, rhs)  self /= rhs    除法
      __ifloordiv__(self, rhs)  self //= rhs    地板除法
      __imod__(self, rhs)    self %= rhs    求余
      __ipow__(self, rhs)    self **= rhs    幂运算

      rhs (right hand side)   右手边

      class MyList:
        def __init__(self, iterable=()):
          self.data = [x for x in iterable]
    
        def __repr__(self):
          return "MyList(%s)" % self.data
    
        def __add__(self, rhs):
          print("__add__")
          L = self.data + rhs.data
          return MyList(L)
    
        # def __iadd__(self, rhs):
        # print("__iadd__")
        # self.data += rhs.data
        # return self
    
      L1 = MyList(range(1, 4))
      L2 = MyList([4, 5, 6])
      print("+= 之前的 id(L1)", id(L1))
      L3 = L1
      L1 += L2
      print("+= 之后的 id(L1)", id(L1))
      print(L1)
      print(L3)
    View Code

    比较运算符的重载
      方法名         运算符和表达式   说明
      __lt__(self, rhs)    self < rhs     小于
      __le__(self, rhs)    self <= rhs    小于等于
      __gt__(self, rhs)    self > rhs     大于
      __ge__(self, rhs)    self >= rhs    大于等于
      __eq__(self, rhs)    self == rhs    等于
      __ne__(self, rhs)    self != rhs    不等于

    注: 比较运算符通常返回布尔值 True 或 False

    位运算符的重载
      方法名          运算符和表达式  说明
      __and__(self, rhs)    self & rhs     位与
      __or__(self, rhs)     self | rhs    位或
      __xor__(self, rhs)    self ^ rhs    位异与
      __lshift__(self, rhs)   self << rhs    左移
      __rshift__(self, rhs)   self >> rhs    右移

    反向位运算符的重载
      方法名          运算符和表达式  说明
      __rand__(self, lhs)    lhs & self    位与
      __ror__(self, lhs)    lhs | self    位或
      __rxor__(self, lhs)    lhs ^ self    位异与
      __rlshift__(self, lhs)  lhs << self    左移
      __rrshift__(self, lhs)  lhs >> self    右移

    复合赋值位运算符的重载
      方法名          运算符和表达式   说明
      __iand__(self, rhs)    self &= rhs    位与
      __ior__(self, rhs)    self |= rhs    位或
      __ixor__(self, rhs)    self ^= rhs    位异与
      __ilshift__(self, rhs)  self <<= rhs    左移
      __irshift__(self, rhs)  self >>= rhs    右移


    一元运算符的重载
      方法名        运算符和表达式  说明
      __neg__(self)    -self      负号
      __pos__(self)    +self      正号
      __invert__(self)   ~self       取反

    一元运算符的重载语法:
      class 类名:
        def __xxx__(self):
          ...

      class MyList:
        def __init__(self, iterable=()):
          self.data = [x for x in iterable]
    
        def __repr__(self):
          return "MyList(%s)" % self.data
    
        def __neg__(self):
          return MyList([-x for x in self.data])
    
      L1 = MyList([1, -2, 3, -4, 5])
      L2 = -L1
      print(L2) # MyList([-1, 2, -3, 4, -5])
    View Code

    in , not in 运算符的重载
      方法名          运算符和表达式  说明
      __contains__(self, e)   e in self     成员运算

      class MyList:
        def __init__(self, iterable=()):
          self.data = [x for x in iterable]
    
        def __repr__(self):
          return "MyList(%s)" % self.data
    
        def __contains__(self, item):
          return item in self.data
    
      L1 = MyList([1, -2, 3, -4, 5])
      if 3 in L1:
        print("")
      else:
        print("")
        print(3 not in L1)
    View Code

    索引和切片运算符的重载:
      重载方法
      方法名          运算符和表达式   说明
      __getitem__(self, i)   x = self    索引/切片取值
      __setitem__(self, i, v) self = v    索引/切片赋值
      __delitem__(self, i)   del self    删除索引/切片

    作用:
      让自定义的类型的对象能够支持索引和切片操作

      class MyList:
        def __init__(self, iterable=()):
          self.data = [x for x in iterable]
    
    
        def __repr__(self):
          return "MyList(%s)" % self.data
    
    
        def __getitem__(self, item):
          print("__getitem__", item)
          return self.data[item]
    
    
        def __setitem__(self, key, value):
          print("__setitem__(key=", key, ',value=', value,')')
          self.data[key] = value
    
    
        def __delitem__(self, key):
          print('正在删除第', key, '个元素')
    
    
      L1 = MyList([1, -2, 3, -4, 5])
      v = L1[2] # 调用 v = L1.__getitem__(2)
      print(v) # 3
      L1[1] = 2 # 调用 L1.__setitem__(1, 2)
      print(L1)
      del L1[3] # 调用 L1.__delitem__(3)
    View Code
      class MyList:
        def __init__(self, iterable=()):
          self.data = [x for x in iterable]
    
        def __repr__(self):
          return "MyList(%s)" % self.data
    
        def __getitem__(self, item):
          print("__getitem__:", item)
          if type(item) is int:
            print("正在做索引操作,item=", item)
          elif type(item) is slice:
            print("正在做切片操作:")
            print("起始值:", item.start)
            print("终止值:", item.stop)
            print("步长:", item.step)
          return self.data[item]
    
    
    
      L1 = MyList([1, -2, 3, -4, 5])
      v = L1[1::2]
      print(v)
      v = L1[3]
      print(v)
      # L1[1:2:3] = [4, 5, 6]
      # L1.__setitem__(slice(1, 2, 3), [4, 5, 6])
    View Code

    slice 构造函数
      作用:
        用于创建一个slice切片对象,此对象存储一个切片的起始值,终止值,步长信息
      格式:
        slice(start=None, stop=None, step=None)
      slice对象的实例属性
        s.start 切片的起始值,默认为None
        s.stop 切片的终止值,默认为None
        s.step 切片的步长,默认为None

     

    特性属性 @property
      实现其它语言所拥有的getter 和 setter功能
    作用:
      用来模拟一个属性
      通过@property装饰器可以对模拟属性赋值和取值加以控制

      class Student:
        def __init__(self, s):
          self.__score = s # 成绩
    
    
        @property
        def score(self):
          '''getter'''
          return self.__score
    
    
        @score.setter
        def score(self, new_score):
          '''setter'''
          assert 0 <= new_score <= 100, '成绩不合法'
          self.__score = new_score
    
    
    
      s1 = Student(50)
      print(s1.score)
      s1.score = 999 # 用setter来控制赋值操作
      print(s1.score)
    View Code

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-14 23:03 , Processed in 0.068575 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表