Python,代码中出现了意外的关键字参数
我的Python代码总是出现这个错误
这是我尝试调用的函数,下面是调用它的代码。
from sys import stdout
def print_nested_list(lijst, indent=False, indent_level=0, fh=stdout):
for x in lijst:
if isinstance(x, list):
print_nested_list(x, indent, indent_level+1, fh)
else:
if indent:
for tabstop in range(indent_level):
print("\t", end='', file=fh)
print(x, file=fh)
try:
with open(r'C:\Python34\headfirstpython\chapter 3\man_data.txt', 'w') as man_data:
print_nested_list(man, fh=man_data)
with open(r'C:\Python34\headfirstpython\chapter 3\other_data.txt', 'w') as other_data:
print_nested_list(other, fh=other_data)
当我尝试运行它时,IDLE给出了这个错误
Traceback (most recent call last):
File "C:\Python34\headfirstpython\chapter 3\sketch1.py", line 25, in <module>
print_nested_list(man, fh=man_data)
TypeError: print_nested_list() got an unexpected keyword argument 'fh'
我不明白我的函数或者调用函数的方式有什么问题?
2 个回答
1
你的函数里没有fh这个关键字参数。不过,它有一个叫fh_id
的关键字参数。
你可以选择修正你的函数定义(把fh_id
改成fh
),或者修正你的调用(用fh_id
代替fh
)。
9
在参数列表中,你没有看到'fh',而是'fh_id'。试着用'fh'来代替它。