同时为一个对象调用多个方法?

时间:2014-08-08 13:32:29

标签: python methods

我刚开始学习python。我想知道为什么在文件对象上执行以下操作会产生错误?

 ex = open(file).seek(10).readline()

我不能为文件对象调用多个方法吗?

1 个答案:

答案 0 :(得分:0)

您没有调用文件对象的多个方法,而是调用文件对象的seek方法,以及seek方法返回的对象的readline。

data.txt的内容:

Hello, world! Hello, world! Hello, world!

main.py的内容:

fname = 'data.txt'

fid = open(fname)

a = fid.seek(10)
print a, type(a)

b = fid.readline()
print b, type(b)

返回:

None <type 'NoneType'>
ld! Hello, world! Hello, world!
<type 'str'>