假设我定义了以下类:class MyClass(object):
def __init__(self, x, y):
self.x = x
self.y = y
通常,可以通过以下方式之一实例化该类:>>> MyClass(1,2)
>>> MyClass(1, y=2)
>>> MyClass(x=1, y=2)
>>> MyClass(y=2, x=1)
很好很漂亮。
现在,我们尝试使用一个无效的关键字参数,看看会发生什么:>>> MyClass(x=1, j=2)
Traceback (most recent call last):
File "", line 1, in
TypeError: __init__() got an unexpected keyword argument 'j'
Python正确地引发了一个类型错误,并抱怨unexpected keyword argument 'j'。
现在,我们可以尝试使用两个无效的关键字参数:>>> MyClass(i=1,j=2)
Traceback (most recent call last):
File "", line 1, in
TypeError: __init__() got an unexpected keyword argument 'i'
注意,其中两个关键字参数是无效的,但