我阅读了 Microsoft 的 With-Statement 文档,但仍然不明白该With语句的目的是什么......
来自 Reddit 的 Xpost认为这确实应该在 Stack Overflow 上的某个地方,但找不到任何此类性质的问题。
我刚刚在我刚刚安装的laravel包中发现了这个,并且真的不知道是什么with()...因为它没有在任何地方声明
public static function of($query)
{
$ins = with(new static); // What is with(new static) ?
$ins->save_query($query);
return $ins;
}
Run Code Online (Sandbox Code Playgroud) 我的想法是否正确,这会自动关闭文件?
def get_file():
with open("file.csv", "rb") as f:
yield f
f = get_file()
do_stuff(f)
Run Code Online (Sandbox Code Playgroud)
如果没有,我如何编写一个返回文件对象的函数,同时确保它在接收器使用完文件后关闭该文件?
如何关闭此文件.任何的想法?
with open('output.txt','w', encoding='UTF-8') as output:
output.writelines(str(i)+'\n' for i in range(5))
Run Code Online (Sandbox Code Playgroud) 此代码运行最佳并且易于理解:
function evalInScope(js, contextAsScope) {
//# Return the results of the in-line anonymous function we .call with the passed context
return function() {
with(this) {
return eval(js);
};
}.call(contextAsScope);
}
evalInScope("a + b", {a: 1, b: 2}); // 3 obviously, but fails in strict mode!
Run Code Online (Sandbox Code Playgroud)
然而,“聪明”的大脑决定删除该with声明,而不进行适当的替换。
问题:如何让它在自动处于严格模式的 ES6 中再次工作?
python ×2
python-3.x ×2
ecmascript-6 ×1
file ×1
javascript ×1
php ×1
static ×1
strict-mode ×1
vba ×1
yield ×1