Discuss / Python / 文章中有个错误

文章中有个错误

Topic source

zicozicozi

#1 Created at ... [Delete] [Delete and Lock User]
def person(name, age, *args, city, job):
    print(name, age, args, city, job)
>>> person('Jack', 24, 'Beijing', 'Engineer')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: person() takes 2 positional arguments but 4 were given

这里怎么会报一个位置参数过多的错误, *args会接收传进去的'Beijing' ,'Engineer',这里的错误应该是缺少了两个命名关键字参数

TypeError: person() missing 2 required keyword-only arguments: 'city' and 'job'     

def person(name, age, *, city, job):
    print(name, age, city, job)

如果是这样命名的,才会报位置参数过多的错误

TypeError: person() takes 2 positional arguments but 4 were given

你是不是看差行了,文章就是这样描述的


  • 1

Reply