在阅读了Pydoc中的文件对象并查看了函数如何调用函数之后,我重写了Zed Shaw的LPTHW脚本之一,试图了解它是如何工作的。调用函数的函数;为什么这会返回NoneType而不是读取文件?
下面是代码:
def open_file(f):
open(f)
def read_file(f):
f.read()
read_file(open_file('test.txt'))
这里是错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in read_file
AttributeError: 'NoneType' object has no attribute 'read'
然而这工作得很好:
input_file = 'test.txt'
print open(input_file).read()
为什么函数调用函数的版本返回NoneType而不是读取文件?
我现在明白了;这是最简单的事情,有时让我。 –