【发布时间】:2016-03-31 18:11:28
【问题描述】:
当我尝试设置继承自 str 的类的参数值时出现错误。仅当我尝试使用myClass(arg = 'test') 访问参数时才会出现该错误。错误是:
TypeError: 'arg' is an invalid keyword argument for this function
这个例子显示了问题:
class A(str):
def __init__(self, arg):
pass
A("test") # Works well
A(arg = "test") # Fails
只有最后一行会引发错误。上一行运行良好。
从int 或float 继承的类也存在同样的问题。
更新(解决方案):
我通过这些链接找到了解决方案:
- Adding optional parameters to the constructors of multiply-inheriting subclasses of built-in types?
- inheritance from str or int
解决办法是:
class A(str):
def __new__(cls, *args, **kwargs):
return str.__new__(cls)
def __init__(self, arg01):
print(arg01)
A(arg01= "test")
我不知道为什么会这样,我会对此进行调查。如果有人有明确的解释,我很感兴趣,我提前感谢他。
更新(我的解释):
我完全不确定我会说什么,但这是我理解的。
想象一个没有任何继承的班级'myClass'。
当我这样做myInstance = myClass() 时,会发生以下情况:
方法myClass.__new__被执行。此方法将创建对象myInstance。 __new__ 是真正的构造函数(__init__ 不是构造函数!)。在伪代码中,__new__ 看起来像这样:
def __new__ (cls, *args, **kwargs):
myInstance = # Do some stuff to create myInstance, an object of the type passed as argument (cls).
# Add the method __init__ to myInstance.
# Call __init__ and pass to it the list 'args' and the dictionary 'kwargs' (both come from arguments of __new__). Pass to it also the object itself (self) :
obj.__init__(self, args, kwargs) :
# Do some stuff.
当我们使用不可变类型(str、int、float、tuple)时,情况会有所不同。在前面的伪代码中,我写了def __new__(cls, *args, **kwargs)。对于不可变类型,方法__new__ 的伪代码更像是def __new__(cls, anUniqueValue)。我真的不明白为什么immutableTypes.__new__ 的行为会尊重其他人,但事实就是如此。你可以在这个例子中看到它:
class foo():
def __init__(self):
pass
foo.__new__(foo, arg = 1)
# That works because the method __new__ look like this : def __new__(*args, **kargs).
str.__new__(str, arg = 1)
# That fails because we are trying to pass the attribute 'arg' to a method which look like this : def __new__(anUniqueValue).
从那里,我们可以理解为什么前面提出的解决方案有效。我们所做的是将不可变类型的方法__new__ 编辑为像可变类型一样工作。
def __new__(cls, *args, **kwargs):
return str.__new__(cls)
这两行将def __new__ (cls, anUniqueValue) 转换为def __new__ (cls, *args, **kwargs)
我希望我的解释几乎是清楚的,没有太多错误。如果您说法语,您可以通过该链接了解更多信息:http://sametmax.com/la-difference-entre-new-et-init-en-python/
【问题讨论】:
-
可能与 stackoverflow.com/questions/3748635/… 有关,尽管我不确定 Alex Martelli 的“同样任意的超类”究竟是什么意思。
-
@Morgan,解释在链接的问题中,也可能有一个她可以为你提供清晰的解释;)
-
@Morgan 当您发现到底发生了什么时,会毫不犹豫地回答您自己的问题。我自己并没有完全理解链接问题中的解释。
-
@Morgan,不费吹灰之力,答案中已经很好地解释了,所以如果你有更简单的解释,你一定要把它写成答案
标签: python class inheritance python-internals