excpt:捕获所有异常
excpt:<异常名>:捕获指定异常
excpt:<异常名1, 异常名2>: 捕获异常1或者异常2
excpt:<异常名>,<数据>:捕获指定异常及其附加的数据
excpt:<异常名1, 异常名2>: <数据>:捕获异常名1或者异常名2,及附加的数据
会继续处理finally中的代码
用raise方法可以抛出自定义异常
read:读取整个文件
readline:读取下一行,使用生成器方法
readlines:读取整个文件到一个生成器已提供我们遍历
input()函数获取用户的输入,不论输入的是什么,获取到的都是字符串类型
import re
secret_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'
b = re.findall('xx.*xx',secret_code) # 贪婪匹配
print (b) # ['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx']
c = re.findall('xx.*?xx',secret_code) # 非贪婪匹配
print(c) # ['xxIxx', 'xxlovexx', 'xxyouxx']
1、贪婪匹配:总是尝试匹配尽可能多的字符
2、非贪婪匹配:是尝试匹配尽可能少的字符
在子类继承多个父类时,属性查找方式粉深度优先和广度优先两种
当类是经典类时,多继承情况下,在要查找属性不存在时,会按照深度优先方式查找下去。
当类时新式类时,多继承情况下,在要查找属性不存在时,会按照广度优先方式查找下去。
enumerate()函数用于将一个可遍历的数据对象(列表、元组和字符串)组合为一个索引序列,同时输出数据和数据下标,一般在for循环中使用。
my_list = ['apple', 'banana', 'grapes', 'pear']
for c, value in enumerate(my_list, 1):
print(c, value)
1 apple
2 banana
3 grapes
4 pear
如果是enumerate(my_list) ,输出的下标从0开始。
pass是空语句,为了保持程序结构的完整性。pass不做任何事情,一般用做占位语句。
使用raise
def test_raise(n):
if not isinstance(n, int):
raise Exception("not a int type")
else:
print("good")
test_raise(8.9)
Exception Traceback (most recent call last)
in
----> 1 test_raise(8.9)
in test_raise(n)
1 def test_raise(n):
2 if not isinstance(n, int):
----> 3 raise Exception("not a int type")
4 else:
5 print("good")
Exception: not a int type
Python的断言就是检测一个条件,如果条件为真,它什么都不做;反之它触发一个带可选错误信息的AssertionError
def testassert(n):
assert n == 2, "n is not 2"
print("n is 2")
testassert(2)
testassert(3)
n is 2
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
in
4
5 testassert(2)
----> 6 testassert(3)
in testassert(n)
1 def testassert(n):
----> 2 assert n == 2, "n is not 2"
3 print("n is 2")
4
5 testassert(2)
AssertionError: n is not 2
同步异步指的是调用者与被调用者之间的关系
阻塞非阻塞是线程或进程之间的关系