在Julia中相当于Python的"with"吗? [英] Equivalent of Python's 'with' in Julia?
本文介绍了在Julia中相当于Python的"with"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Julia是否具有与Python的 with
等效的功能?也许作为宏?例如,这对于自动关闭打开的文件非常有用.
Does Julia have an equivalent of Python's with
? Maybe as a macro? This is very useful, for example, to automatically close opened files.
推荐答案
使用 do
块.此处.
这是一个如何在Julia中使用open(filename)作为Python的my_file 进行常规的示例:
And here is an example of how to do the usual with open(filename) as my_file
of Python in Julia:
open("sherlock-holmes.txt") do filehandle
for line in eachline(filehandle)
println(line)
end
end
上面的示例也来自 Julia Wikibooks .
这篇关于在Julia中相当于Python的"with"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文