Python抽象及异常处理
#面向对象:多态、封装、继承#1 多态:意味着即使不知道变量所引用的对象类型是什么,还是能对他进行操作,而且也会根据他的不同类型表现出不同的行为#多态和方法 from random import choicex = choice(['Hello World',['1','1','1','2','3']])print(x)print(x.count('1'))#1出现的次数 不需要检测类型 只需要知道有conut这个方法#多态的多种形式:+运算对字符串、数字起作用 #2 封装对全局作用域中的其他区域隐藏多余信息#3 继承:不想代码重复 使用原来的代码#4 类#创建类_metaclass_= typeclass Person:def setname(self,name):self.name = namedef getname(self):return self.namedef greet(self):print("Hello I am %s"%self.name)#调用类foo = Person()bar = Person()foo.setname("1234567")bar.setname("7891213")foo.greet()bar.greet()print(foo.name,bar.name)#-------------------------输出结果----------------------------------#"""C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.pyHello World0Hello I am 1234567Hello I am 78912131234567 7891213""" #特性、函数、方法class Class:def method(self):print("I am self")def fuction():print("I don't...") obj = Class()obj.method()obj.method = fuction # 特性绑定到一个普通的函数上obj.method() test = obj.method #test变量引用绑定方法上test() #-------------------------输出结果----------------------------------#"""I am selfI don't...I don't..""" #私有特性:为了让特性和方法变成私有(外部不能访问),只要在名字前加上双下划线#指定超类class Filter:def init(self):self.blocked=[]def filter(self,test):return [x for x in test if x not in self.blocked]def _test(self):print("我是私有的")class ChildFilter(Filter):#指定超类 ChildFilter为Filter的子类 def init(self):print("我是子类 正在重写父类方法")self.blocked=['as']f = Filter()f.init()print(f.filter([1,2,3])) f1 = ChildFilter()f1.init()print(f1.filter([1,23,56])) #-------------------------输出结果----------------------------------#"""C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.py[1, 2, 3]我是子类 正在重写父类方法[1, 23, 56]""" #捕获异常try: x = 5 y = 0 print(x/y)except ZeroDivisionError:print("分母不能为0")#-------------------------输出结果----------------------------------#"""C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.py分母不能为0""" # raiseclass MuffledCalculator: muffed = False def calc(self,expr):try:return eval(expr)except ZeroDivisionError:if self.muffed:print("Division is zero by illegal")else:raise #cal = MuffledCalculator()print(cal.calc('10/2'))cal.muffed = True #打开屏蔽机制print("hehehe-->",cal.calc('10/0'))#-------------------------输出结果----------------------------------#"""C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.py分母不能为05.0Division is zero by illegalhehehe--> None""" #用一个块捕捉两个异常try: x = input("input your first num:") y = input("input your second num:")print(x/y)except(ZeroDivisionError,TypeError,NameError).e:print("Your Num were bug")print(e) while True:try: x = int(input("input your first num:")) y= int(input("input your second num:")) value = x/yprint(value)except Exception.e:print("Exception ",e)print("try again")else:break """#-------------------------输出结果----------------------------------#C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.pyinput your first num:10input your second num:33.3333333333333335C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.pyinput your first num:10input your second num:0Traceback (most recent call last): File "D:/Python-Test/qiubai/qiubai/Test6.py", line 145, in <module> value = x/yZeroDivisionError: division by zeroDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "D:/Python-Test/qiubai/qiubai/Test6.py", line 147, in <module> except Exception.e:AttributeError: type object 'Exception' has no attribute 'e'"""while True: try: x = int(input("input your first num:")) y= int(input("input your second num:")) value = x/y print(value) except: print("try again") else:#只有程序没有异常情况才会退出 只要错误发生,程序会不断重新要求输入 break"""#-------------------------输出结果----------------------------------#C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.pyinput your first num:10input your second num:0try againinput your first num:10input your second num:33.3333333333333335"""#Finally:x = Nonetry: x = x / 0finally:#通常用于关闭文件或网络套接字 print('Clean up .....') del x #程序崩溃之前 对于x的变量清理 """C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test6.pyClean up .....Traceback (most recent call last): File "D:/Python-Test/qiubai/qiubai/Test6.py", line 203, in <module> x = x / 0TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'"""
转载于:https://www.cnblogs.com/acer-haitao/p/7304903.html
Python抽象及异常处理相关推荐
- python网络编程-异常处理-异常捕获-抛出异常-断言-自定义异常-UDP通信-socketserver模块应用-03
python网络编程-异常处理-异常捕获-抛出异常-断言-自定义异常-UDP通信-socketserver模块应用-03 参考文章: (1)python网络编程-异常处理-异常捕获-抛出异常-断言-自 ...
- python基础之异常处理
python基础之异常处理 参考文章: (1)python基础之异常处理 (2)https://www.cnblogs.com/zhangyux/p/6108026.html (3)https://w ...
- python错误-python错误和异常处理怎处理你知道么
原标题:python错误和异常处理怎处理你知道么 异常处理 什么是异常? 首先要清楚,什么是异常,异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常 ...
- python基础语法-异常处理
python中的异常处理 类似于java 开发中很常用 异常处理的好处 大大减少了由于异常程序崩溃的发生. 三个基本语句 try:在try语句后放入可能出现问题的代码,没有问题正常执行 except: ...
- Python中的异常处理Try...except...finally的使用的简单理解
python中的异常处理 1.Python中的异常处理定义 2.Try-except-finally的使用 3.代码 4.运行结果 1.Python中的异常处理定义 在程序运行的过程中,如果发生了错误 ...
- python学习总结----异常处理
python学习总结----异常处理 相关概念- 错误:程序运行之前的语法错误,如:关键字.缩进不齐.括号不成对. - 异常:在程序运行过程中出现的问题,如:除数为0.对象属性不存在等.异常处理- 说 ...
- Python Selenium之异常处理
Python Selenium之异常处理 参考文章: (1)Python Selenium之异常处理 (2)https://www.cnblogs.com/cnkemi/p/8985654.html ...
- Python基础:异常处理
Python基础:异常处理 一,学习的内容 Python 异常处理 异常就是运行期检测到的错误.计算机语言针对可能出现的错误定义了异常类型,某种错误引发对应的异常时,异常处理程序将被启动,从而恢复程序 ...
- Python异常和异常处理
Python中的异常处理与Java中的做法思路类似,个别细节的地方需要注意下即可,理解起来没有太大问题 try-except块及finally 异常常用小技巧: (1)在类型转换的地方检查类型转换是否 ...
最新文章
- 第十二篇:形式语言理论与有限状态自动机
- leetcode算法题--将数字变成 0 的操作次数
- scala 函数调用_在Scala中按名称调用函数
- Pro ASP.NET 4 CMS
- CV Code|计算机视觉开源周报20200501期
- javascript实现的复制到剪贴板
- 2018麦考林杂志计算机科学,最新出炉|2018年麦考林杂志加拿大大学排名!
- jQuery.extend()、jQuery.fn.extend()扩展方法具体解释
- catia保存成stp文件时部件丢失_超详细的分布式文件系统FastDFS磁盘恢复介绍
- linux中终端进入目录太多时造成终端路径显示太长的路径影响观看的解决方法
- win10系统下载文件、解压缩文件时文件名称乱码的问题解决方法
- 吴恩达深度学习课程第五章第二周编程作业(pytorch实现)
- Android聊天室(源码)
- 打开计算机管理iis,iis管理器怎么打开?Win7打开iis管理器的操作方法
- 天池比赛——docker初步尝试
- 显卡检测神器 GPU-Z v2.30.0 简体中文汉化版
- VML编辑器代码实现(转载)
- 什么是内部类?内部类的作用
- 计算机二级Java考试笔记
- 码代码时遇到的小插曲