我有一个python文件:
# -*- coding: utf-8 -*-
from bead import Ford
szotar = {"The" : "A", "sun": "nap", "shining" : "süt", "wind" : "szél", "not" : "nem", "blowing" : "fúj"}
fd = Ford(szotar)
fd.fordit("teszt.txt")
我必须编写Ford类,它有一个fordit函数,打开作为参数传递的文件。我写道:
class Ford(dict):
def fordit(read):
fajl = open(read)
for sor in fajl:
print(sor)
fajl.close()
但是我得到了错误“TypeError:fordit()正好接受1个参数(给定2个)”。有什么问题
您没有使用必要的参数定义
fordit
f.fordit(x)
相当于Ford.fordit(f, x)
,因此需要将其定义为接受两个参数,第一个是调用方法的对象,通常(但不一定)命名为self
(不相关,但您应该使用
with
语句,该语句确保即使在文件打开时发生错误也会关闭文件。一旦with
语句完成,文件将隐式关闭。)相关问题 更多 >
编程相关推荐