# Open new file to write
file = None
try:
file = open(filePath, 'w')
except IOError:
msg = ("Unable to create file on disk.")
file.close()
return
finally:
file.write("Hello World!")
file.close()
Run Code Online (Sandbox Code Playgroud)
上面的代码是从函数中删除的.用户的一个系统正在报告错误:
file.write("Hello World!")
Run Code Online (Sandbox Code Playgroud)
错误:
AttributeError: 'NoneType' object has no attribute 'write'
Run Code Online (Sandbox Code Playgroud)
问题是,如果python未能打开给定文件,'except'块执行并且必须返回,但是控制将转移到抛出给定错误的行.'file'变量的值为'None'.
有什么指针吗?
我想关闭stdout的缓冲以获取以下代码的确切结果
while(1) {
printf(".");
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
代码printf一堆'.' 只有当缓冲区被填满时
我写了一个更高阶的Haskell函数如下,
higherOrderFun f p xs = (map f) (filter p xs)
Run Code Online (Sandbox Code Playgroud)
它适用于以下情况
higherOrderFun (\x -> 2 * x) odd [1..4]
but throws an error for
higherOrderFun sin odd [1..4]
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
No instance for (Show b0) arising from a use of ‘print’
The type variable ‘b0’ is ambiguous
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’ …Run Code Online (Sandbox Code Playgroud)