Python,在我的 Python 代码中有一个意外的关键字参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23523747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:04:50  来源:igfitidea点击:

Python, got an unexpected keyword argument in my python code

pythonfunctionkeyword

提问by user3263958

My python code keeps giving me this error

我的 python 代码不断给我这个错误

this is the function I try to call with underneath it the code that calls it.

这是我尝试在其下方调用调用它的代码的函数。

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 gives this error when i try to run it

当我尝试运行 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'

I don't understand what is wrong with my function or the way I call my function?

我不明白我的函数或我调用函数的方式有什么问题?

回答by Nathaniel Flath

In the argument list, you don't have 'fh' - you have 'fh_id'. Try using 'fh' instead.

在参数列表中,你没有“fh”——你有“fh_id”。尝试使用 'fh' 代替。

回答by Martijn Pieters

Your function doesn't havea fhkeyword argument. It has a fh_idkeyword argument though.

您的函数不具有一个fh关键字参数。fh_id虽然它有一个关键字参数。

Either fix your function signature (rename fh_idto fh) or your call (use fh_idinstead of fh).

修复您的函数签名(重命名fh_idfh)或您的调用(使用fh_id而不是fh)。