我with
今天第一次遇到了Python 语句.我已经使用Python几个月了,甚至不知道它的存在!鉴于其地位有点模糊,我认为值得问:
with
设计用于的Python 语句? try..finally
比它更好用的情况with
?是否可以使用with
Python中的语句声明多个变量?
就像是:
from __future__ import with_statement
with open("out.txt","wt"), open("in.txt") as file_out, file_in:
for line in file_in:
file_out.write(line)
Run Code Online (Sandbox Code Playgroud)
......还是正在清理两个资源同时出现问题?
我在某人的代码中看到了这个.这是什么意思?
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self.stream.close()
Run Code Online (Sandbox Code Playgroud)
from __future__ import with_statement#for python2.5
class a(object):
def __enter__(self):
print 'sss'
return 'sss111'
def __exit__(self ,type, value, traceback):
print 'ok'
return False
with a() as s:
print s
print s
Run Code Online (Sandbox Code Playgroud) 考虑以下:
with open(path, mode) as f:
return [line for line in f if condition]
Run Code Online (Sandbox Code Playgroud)
文件是否会正确关闭,或者以return
某种方式绕过上下文管理器?
假设您有三个通过上下文管理器获取的对象,例如A锁,数据库连接和ip套接字.您可以通过以下方式获取它
with lock:
with db_con:
with socket:
#do stuff
Run Code Online (Sandbox Code Playgroud)
但有没有办法在一个街区内完成?就像是
with lock,db_con,socket:
#do stuff
Run Code Online (Sandbox Code Playgroud)
此外,如果有一组具有上下文管理器的未知长度的对象,是否有可能以某种方式做到:
a=[lock1, lock2, lock3, db_con1, socket, db_con2]
with a as res:
#now all objects in array are acquired
Run Code Online (Sandbox Code Playgroud)
如果答案是"不",是不是因为需要这样的功能意味着设计不好,或者我应该建议它?:-P
如何使用模拟测试以下代码(使用模拟,修补程序装饰器和Michael Foord的Mock框架提供的标记):
def testme(filepath):
with open(filepath, 'r') as f:
return f.read()
Run Code Online (Sandbox Code Playgroud) 这是将python"with"语句与try-except块结合使用的正确方法吗?:
try:
with open("file", "r") as f:
line = f.readline()
except IOError:
<whatever>
Run Code Online (Sandbox Code Playgroud)
如果是,那么考虑旧的做事方式:
try:
f = open("file", "r")
line = f.readline()
except IOError:
<whatever>
finally:
f.close()
Run Code Online (Sandbox Code Playgroud)
这里"with"语句的主要好处是我们可以摆脱三行代码吗?对于这个用例来说,这似乎并不令我感到高兴(尽管我理解"with"语句还有其他用途).
编辑:上面两个代码块的功能是否相同?
EDIT2:前几个答案一般性地讨论了使用"with"的好处,但这些似乎在边缘效益.我们已经(或应该已经)多年来明确地调用f.close().我想一个好处是,草率编码器将从使用"with"中受益.
在工作中,我经常在一些项目中工作,这些项目必须在建造期间或生命早期设置某些物体的许多属性.为了方便和可读性,我经常使用该With
语句来设置这些属性.我发现
With Me.Elements
.PropertyA = True
.PropertyB = "Inactive"
' And so on for several more lines
End With
Run Code Online (Sandbox Code Playgroud)
看起来好多了
Me.Elements.PropertyA = True
Me.Elements.PropertyB = "Inactive"
' And so on for several more lines
Run Code Online (Sandbox Code Playgroud)
对于只设置属性的很长的语句.
我注意到With
在调试时使用时存在一些问题; 但是,我想知道是否有任何令人信服的理由避免With
在实践中使用?我总是假设通过编译器为上述两种情况生成的代码基本相同,这就是为什么我总是选择写出我觉得更具可读性的原因.
有没有办法用with语句开始一段代码,但是有条件地?
就像是:
if needs_with():
with get_stuff() as gs:
# do nearly the same large block of stuff,
# involving gs or not, depending on needs_with()
Run Code Online (Sandbox Code Playgroud)
为了澄清,一种情况会在with语句中包含一个块,而另一种情况可能是同一个块,但不包含(即,好像它没有缩进)
最初的实验当然会给出压痕错误..
python conditional indentation with-statement conditional-statements
with-statement ×10
python ×8
conditional ×1
except ×1
finally ×1
indentation ×1
javascript ×1
mocking ×1
oop ×1
return ×1
try-catch ×1
vb.net ×1