简体   繁体   English

Python TypeError: got an unexpected keyword argument 'name'

[英]Python TypeError: got an unexpected keyword argument 'name'

I am new to python and recently I just learned the basic of *args and **kwargs.我是 python 的新手,最近我刚刚学习了 *args 和 **kwargs 的基础知识。

When I try to practice with my own code, I got an error of unexpected keyword as shown below:当我尝试使用自己的代码进行练习时,出现了一个意想不到的关键字错误,如下所示:

def student_info2(args, kwargs):
    print(args)
    print(kwargs)

courses = ["Maths", "Statistics"]
module = "Data Science"
students = {"name": "John Price", "age": 27}
welcome_words = {"welcome": "hello and welcome"}
student_info2(*courses, *module, **students, **welcome_words)
TypeError                                 Traceback (most recent call last)
<ipython-input-61-8bc2f643a250> in <module>
      7 students = {"name":"John Price","age":27}
      8 welcome_words = {"welcome":"hello and welcome"}
----> 9 student_info2(*courses,*module, **students, **welcome_words)

TypeError: student_info2() got an unexpected keyword argument 'name'

I really have no idea why this is happening, really appreciated if someone could help me solve this problem.我真的不知道为什么会这样,如果有人可以帮助我解决这个问题,我真的很感激。

def student_info2(args,kwargs):
    print(args)
    print(kwargs)

This function will work just fine, but it will be limited to only 2 arguments namely args and kwargs .这个 function 可以正常工作,但仅限于 2 个 arguments 即argskwargs At this point the args and kwargs are just 2 variables names, they will not act as you intend them to because you have not used them with unpacking operator like *args and **kwargs .此时argskwargs只是 2 个变量名称,它们不会按照您的意愿行事,因为您没有将它们与*args**kwargs之类的解包运算符一起使用。

*args and **kwargs are used to give function varying number of input arguments. *args**kwargs用于为 function 提供不同数量的输入 arguments。 args , kwargs is just a variable name that can be changed to whatever you need it to be. argskwargs只是一个变量名,可以更改为您需要的任何名称。

def student_info2(*args, **kwargs):
    print(args)
    print(kwargs)

NOTE:笔记:

  • *args accepts iterables or positional arguments and **kwargs accepts keyword or named arguments. *args接受迭代或位置 arguments 和**kwargs接受关键字或命名 arguments。
  • *args must come before **kwargs when defining the function parameters.定义 function 参数时, *args必须在**kwargs之前。

You don't need *s there, you should put them in the declaration of your function.你不需要 *s,你应该把它们放在你的 function 的声明中。 Also if you give a function 4 parameters, you should receive 4.此外,如果您给 function 4 个参数,您应该收到 4 个。

def student_info2(arg1, arg2, arg3, arg4):
    print(arg1)
    print(arg2)
    print(arg3)
    print(arg4)
    

courses = ["Maths","Statistics"]
module = "Data Science"
students = {"name":"John Price","age":27}
welcome_words = {"welcome":"hello and welcome"}
student_info2(courses, module, students, welcome_words)

If you would like to receive X amount of parameters, you should do如果你想接收 X 数量的参数,你应该这样做

def student_info2(*args):
    print(args)
    

courses = ["Maths","Statistics"]
module = "Data Science"
students = {"name":"John Price","age":27}
welcome_words = {"welcome":"hello and welcome"}
student_info2(courses, module, students, welcome_words)

Where args will be a list of parameters.其中args将是参数列表。 **kwargs will be a dictionary instead of a list. **kwargs将是字典而不是列表。 Also you can mix normal parameters, and args/kwargs.您也可以混合使用普通参数和 args/kwargs。

Check this site for more clarification: https://www.geeksforgeeks.org/args-kwargs-python/检查此站点以获取更多说明: https://www.geeksforgeeks.org/args-kwargs-python/

what you are doing wrong here is你在这里做错的是

your function student_info2 can take only 2 positional arguments by your code, so you should call the function like this您的 function student_info2 只能通过您的代码获取 2 个位置 arguments,因此您应该像这样调用 function

student_info2(courses,module) student_info2(课程,模块)

if you want all 4 to be called in one go, then your function declaration should like this如果您希望在一个 go 中调用所有 4 个,那么您的 function 声明应该像这样

student_info2(arg1, arg2, arg3, arg4) or student_info2(*args) student_info2(arg1, arg2, arg3, arg4) 或 student_info2(*args)

You have two parameters as input for the function, but you gave four arguments as input to the function, but if you want to use args and kwargs, you should not use this way.您有两个参数作为 function 的输入,但您给了四个 arguments 作为 function 的输入,但如果您不想使用 args 和 kwargs,请使用这种方式。 see these site they can help you to undrestand看看这些网站,他们可以帮助你理解

https://realpython.com/python-kwargs-and-args/ https://realpython.com/python-kwargs-and-args/

https://www.programiz.com/python-programming/args-and-kwargshttps://www.programiz.com/python-programming/args-and-kwargs

https://www.geeksforgeeks.org/args-kwargs-python/ https://www.geeksforgeeks.org/args-kwargs-python/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:得到了意外的关键字参数“ name” - TypeError: got an unexpected keyword argument “name” 类型错误:Planet() 有一个意外的关键字参数“名称” - TypeError: Planet() got an unexpected keyword argument 'name' / Accept() 处的 TypeError 得到了意外的关键字参数“名称” - TypeError at / Accept() got an unexpected keyword argument 'name' TypeError:得到一个意外的关键字参数 - TypeError: got an unexpected keyword argument 错误类型错误:urlretrieve()得到了意外的关键字参数&#39;CablingFilename&#39;python - got error TypeError: urlretrieve() got an unexpected keyword argument 'CablingFilename' python 类型错误:__init__() 得到了意外的关键字参数“名称”-CONVOKIT - TypeError: __init__() got an unexpected keyword argument 'name' - CONVOKIT TypeError:url()得到一个意外的关键字参数&#39;name_space&#39; - TypeError: url() got an unexpected keyword argument 'name_space' Keras: TypeError: __call__() 得到了一个意外的关键字参数“name” - Keras: TypeError: __call__() got an unexpected keyword argument 'name' TypeError: login() 得到了一个意外的关键字参数 'template_name' - TypeError: login() got an unexpected keyword argument 'template_name' 错误-TypeError在Django中获得了意外的关键字参数&#39;name&#39; - Error - TypeError got an unexpected keyword argument 'name' in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM