try-except语句:
try:
检测范围
except Exception[as reason]:
出现异常后的处理代码
#f = open('我为什么是一个文件.txt')当这个txt不存在时,FileNotFoundError #print(f.read()) #f.close() try: f = open('我为什么是一个文件.txt') print(f.read()) f.close() except OSError: print('文件出错啦!') try: f = open('我为什么是一个文件.txt') print(f.read()) f.close() except OSError as reason: print('文件出错啦!\n错误的原因是:' + str(reason)) 文件出错啦! 错误的原因是:[Errno 2] No such file or directory: '我为什么是一个文件.txt' try: sum = 1 + '1' f = open('我为什么是一个文件.txt') print(f.read()) f.close() except OSError as reason: print('文件出错啦!\n错误的原因是:' + str(reason)) except TypeError as reason: print('类型出错啦!\n错误的原因是:' + str(reason)) #两个print error只打印最后一个! 类型出错啦! 错误的原因是:unsupported operand type(s) for +: 'int' and 'str' try-finally语句:try:
检测范围
except Exception[as reason]:
出现异常后的处理代码
finally:无论如何都会被执行的代码
try: f = open('我为什么是一个文件.txt') print(f.write('我存在了!')) sum = 1 + '1' f.close() except OSError as reason: print('文件出错啦!\n错误的原因是:' + str(reason)) except TypeError as reason: print('类型出错啦!\n错误的原因是:' + str(reason)) 文件出错啦! 错误的原因是:[Errno 2] No such file or directory: '我为什么是一个文件.txt' try: f = open('我为什么是一个文件.txt','w') print(f.write('我存在了!')) sum = 1 + '1' except OSError as reason: print('文件出错啦!\n错误的原因是:' + str(reason)) except TypeError as reason: print('类型出错啦!\n错误的原因是:' + str(reason)) finally: f.close() 5 类型出错啦! 错误的原因是:unsupported operand type(s) for +: 'int' and 'str' raise语句 >>> 1/0 Traceback (most recent call last): File "<pyshell#73>", line 1, in <module> 1/0 ZeroDivisionError: division by zero >>> raise ZeroDivisionError Traceback (most recent call last): File "<pyshell#74>", line 1, in <module> raise ZeroDivisionError ZeroDivisionError >>> raise ZeroDivisionError('除数为零的异常') Traceback (most recent call last): File "<pyshell#76>", line 1, in <module> raise ZeroDivisionError('除数为零的异常') ZeroDivisionError: 除数为零的异常034 丰富的else和简洁的with语句 def showMaxFactor(num): count = num // 2 while count > 1: if num % count == 0: print('%d最大的约数是%d' % (num,count)) break count -= 1 else: print('%d是素数!' % num) num = int(input('请输入一个数:')) showMaxFactor(num) 请输入一个数:11 11是素数! try: int('abc') except ValueError as reason: print('出错啦:' + str(reason)) 出错啦:invalid literal for int() with base 10: 'abc' try: print(int('123')) except ValueError as reason: print('出错啦:' + str(reason)) else: print('没有任何异常!') 123 没有任何异常! with语句 try: f = open('data.txt', 'w') for each_line in f: print(each_line) except OSError as reason: print('出错啦:' + str(reason)) finally: f.close() 出错啦:not readable try: with open('data.txt', 'w') as f: for each_line in f: print(each_line) except OSError as reason: print('出错啦:' + str(reason)) 出错啦:not readable
对象=属性【静态】+方法【动态】
class Turtle:# Python中的类名约定以大写字母开头 """关于类的一个简单例子""" #属性 color = 'green' weight = 10 legs = 4 shell = True mouth = '大腿' #方法 def climb(self): print('我正在很努力的向前爬。。。') def run(self): print('我正在飞快的向前跑。。。') def bite(self): print('咬死你咬死你!') def eat(self): print('有的吃,真满足') def sleep(self): print('困了,睡了,晚安,Zzzz') >>> tt = Turtle() >>> Turtle() <__main__.Turtle object at 0x0213D0F0> >>> tt.climb() 我正在很努力的向前爬。。。 >>> tt.bite() 咬死你咬死你! >>> tt.sleep() 困了,睡了,晚安,Zzzz >>> oo的特征:Object Oriented面向对象 >>> list1 = [2, 1, 7, 5, 3] >>> list1.sort() >>> list1 [1, 2, 3, 5, 7] >>> list1.append(9) >>> list1 [1, 2, 3, 5, 7, 9] >>> #第一点封装、第二点继承 >>> class MyList(list): pass >>> list2 = MyList() >>> list2.append(5) >>> list2.append(3) >>> list2 [5, 3] >>> list2.sort() >>> list <class 'list'> >>> list2 [3, 5] >>> #第三点为多态 >>> #第三点为多态 >>> class A: def fun(self): print("我是小A") >>> class B: def fun(self): print('我是小B') >>> a = A() >>> b = B() >>> a.fun() 我是小A >>> b.fun() 我是小Bself是什么?相当于C++中this指针,
>>> class Ball: def setName(self, name): self.name = name def kick(self): print('我叫%s,别提我。。' % self.name) >>> a = Ball() >>> a.setName('球A') >>> b = Ball() >>> b.setName('球B') >>> c = Ball() >>> c.setName('土豆') >>> a.kick() 我叫球A,别提我。。 >>> c.kick() 我叫土豆,别提我。。 魔方方法:__init__(self) >>> class Ball: def __init__(self, name): self.name = name def kick(self): print('我叫%s,别提我' % self.name) >>> b = Ball('土豆') >>> b.kick() 我叫土豆,别提我公有和私有?在python中定义私有变量只需在变量名或函数名前加上“_”两个下划线,这个函数就私有。_类名__变量名
>>> class Person: name = '小甲鱼' >>> p = Person() >>> p.name '小甲鱼' >>> class Person: __name = '小甲鱼' >>> p = Person() >>> p.__name Traceback (most recent call last): File "<pyshell#77>", line 1, in <module> p.__name AttributeError: 'Person' object has no attribute '__name' >>> p.name Traceback (most recent call last): File "<pyshell#78>", line 1, in <module> p.name AttributeError: 'Person' object has no attribute 'name' >>> class Person: __name = '小甲鱼' def getName(self): return self.__name >>> p = Person() >>> p.__name Traceback (most recent call last): File "<pyshell#85>", line 1, in <module> p.__name AttributeError: 'Person' object has no attribute '__name' >>> p.getName() '小甲鱼'
