with用法
虽然是基础但是在课堂上,老师对于,with的用法也只是寥寥草草的几个 with open.
下面是对with比较全面的解释
with : 一般来说,想要被with调用需要 enter()与 exit()
举个栗子@。@
1
2
3
4
5
6
7
8
9
10
11
12
13
class duixiang:
def __enter__(self):
print('进入enter方法')
return 6666
def __exit__(self, exc_type, exc_val, exc_tb):#注意到这几个参数exc_.异常0.o有没有像谐音
print('进入exit方法')
def __zhix__(self):
print("执行中")
return 8888
def function():
return duixiang()
with function():
print(function().__zhix__())
### with as f f得到什么呢?
1
2
with function() as f:
print(f)#6666
### 是的,as f,f得到的是enter的返回值
1 | class duixiang: |
1 | with function() as f: |
ok 说回exit中几个特殊的返回值
1 | class duixiang: |
for_else用法
for_else 没学过吧,我也没学过 =。=
上代码:
1 | def fun1(): |
结合contine和break呢
判断你输入的数,是否有目标值
1 | def input_list_int(list_str):#方法1 |
另外的
1 | ##突然想起,如何输入一个整数列表? |